擅长:python、mysql、java
<p>如果<code>entries</code>是一个字符串列表,其中可能包含关键字,也可能不包含关键字,并且顺序很重要,那么我们需要对这些条目进行迭代:</p>
<p>第一种选择:</p>
<pre><code>import re
entries = [ "['data'] runtime: ###, scan: ###",
"['data'] ctime: ###, scan: ###",
"['data'] runtime: ###" ]
allterms = (["runtime", "runtime\s?:\s?([a-zA-Z0-9_#]*)"],
["ctime", "ctime\s?:\s?([a-zA-Z0-9_#]*)"],
["scan", "scan\s?:\s?([a-zA-Z0-9_#]*)"])
terms = [allterms[i][0] for i in range(len(allterms))]
patterns = [allterms[i][1] for i in range(len(allterms))]
def get_terms(entry):
for i in range(len(terms)):
term = re.search(patterns[i], entry)
term = term.groups()[0] if term else None
d[terms[i]] += [term]
pass
d = {t: [] for t in allterms}
for entry in entries:
get_terms(entry)
</code></pre>
<p>第二个异步选项:</p>
<pre><code># pip install futures # if using Python 2
from concurrent.futures import ThreadPoolExecutor
d = {t: [] for t in allterms}
with ThreadPoolExecutor() as executor:
for entry in entries:
get_terms(entry)
</code></pre>
<p>编辑:与@Wynne:合作开发的聊天解决方案)</p>