擅长:python、mysql、java
<p>这应该在不使用正则表达式的情况下工作。为了清楚地说明正在发生的事情,我发表了这些评论。你知道吗</p>
<pre><code>from collections import Counter
my_list = ['Gold Trophy (January)', 'Gold Trophy (February)', 'Bronze Trophy (March)']
output_ls = []
trophy_ls = []
month_ls = []
trophy_cnt_dc = {}
for item in my_list:
trophy_ls.append(item.split(' (')[0])
month_ls.append(item.split(' (')[1])
# print(trophy_ls) >> ['Gold Trophy', 'Gold Trophy', 'Bronze Trophy']
# print(month_ls) >> ['January)', 'February)', 'March)']
trophy_cnt_dc = dict(Counter(trophy_ls))
#print(trophy_cnt_dc) >> {'Gold Trophy': 2, 'Bronze Trophy': 1}
for k,v in trophy_cnt_dc.items():
if v > 1:
output_ls.append(k+' x'+str(v))
else:
ind = trophy_ls.index(k)
output_ls.append(k+' ('+month_ls[ind])
print(output_ls)
</code></pre>
<p>输出:</p>
<pre><code>['Gold Trophy x2', 'Bronze Trophy (March)']
</code></pre>