<p><strong>这里有一个函数,允许您解析字符串中的相关值:</strong></p>
<pre><code>def string_to_list(string):
"""
parses a parent string for 4 character children strings
returns a list of children strings
"""
# instantiate values
child = ''
children = []
if len(string)<4:
return None
for n in string:
# skip if not wanted
if n in ['|','&']:
continue
child+=n
if len(child)==4:
children.append(child)
child = ''
# finished
return children
</code></pre>
<p><strong>应用它来提取值列表,如下所示:</strong></p>
<pre><code>df['Code_List'] = df['Code'].apply(string_to_list)
</code></pre>
<p><strong>映射到相关的<code>logic</code>值:</strong></p>
<pre><code># Instantiate the dictionary of logic rules
logic_dict = {'00FQ':['A','B'], '8LK9':['X'], '4F5H':['U','Z'], '2RLA':['H','K'], 'R1T5':['B','G'] }
# Map the logic rules
df['Logic_List'] = df['Code_List'].apply(lambda arr: [logic_dict[x] for x in arr])
# Final output
ID Code Code_List Logic_List
0 ABCD 00FQ [00FQ] [[A, B]]
1 JKFA 8LK9|4F5H [8LK9, 4F5H] [[X], [U, Z]]
2 QWST 2RLA|R1T5&8LK9 [2RLA, R1T5, 8LK9] [[H, K], [B, G], [X]]
</code></pre>