擅长:python、mysql、java
<p>这是一种简单的方法:</p>
<pre><code>>>> my_string = "COG1: aomo|At1g01190 aomo|At1g01280 aomo|At1g11600 homo|Hs10834998 homo|Hs13699816 "
>>> a,b = my_string.split(":") # will split strings on ":"
>>> a
'COG1'
>>> b
' aomo|At1g01190 aomo|At1g01280 aomo|At1g11600 homo|Hs10834998 homo|Hs13699816 '
>>> import re
>>> from collections import Counter
>>> my_count = Counter(re.findall("aomo|homo|somo",b)) # findall will find all, and Counter will give dictionary of for count of each element
>>> my_count
Counter({'aomo': 3, 'homo': 2})
>>> "{} {} {} {}".format(a,my_count.get('aomo',0),my_count.get('homo',0),my_count.get('somo',0))
'COG1 3 2 0'
</code></pre>