Skip to content

Instantly share code, notes, and snippets.

@bluenex
Last active March 13, 2019 04:34
Show Gist options
  • Save bluenex/b5d5a5571301b69d0180c5bee798dfad to your computer and use it in GitHub Desktop.
Save bluenex/b5d5a5571301b69d0180c5bee798dfad to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"given = 'ca407[-B4-C2-C3] ca408[-B4-B5-C2]'\n",
"result = ['ca407-B4', 'ca407-C2', 'ca407-C3', 'ca408-B4', 'ca408-B5', 'ca408-C2']"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['ca407-B4', 'ca407-C2', 'ca407-C3', 'ca408-B4', 'ca408-B5', 'ca408-C2']"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def map_self(s):\n",
" # pattern to extract\n",
" pattern = r'([a-c]{2}[0-9]{3})(.*)'\n",
" get = re.compile(pattern)\n",
"\n",
" front = list(get.search(s).groups())[0]\n",
" raw_back = list(get.search(s).groups())[1]\n",
" back = [x for x in raw_back.replace('[', '').replace(']', '').split('-') if x]\n",
"\n",
" return [f'{front}-{each}' for each in back]\n",
"\n",
"def flatten_list(nested_list):\n",
" # https://stackoverflow.com/a/952952\n",
" return [item for sublist in nested_list for item in sublist]\n",
"\n",
"flatten_list([map_self(x) for x in given.split()])\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@bachkukkik
Copy link

work like a charm. Thanks krub God Tul

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment