擅长:python、mysql、java
<p>这是一个解决方案(请参阅注释以获得澄清)。注意,我使用了一个小技巧来拆分姓名和日期:我在<code>(</code>上拆分,然后在需要时还原它。可以做得更干净,但不清楚是否需要。你知道吗</p>
<pre><code>my_list = ['Gold Trophy (January)', 'Gold Trophy (February)', 'Bronze Trophy (March)']
# Create map of tuples: (name, date)
pairs = [tuple(x.split('(')) for x in my_list]
# count the number of each name
counts = dict()
for (name, day) in pairs:
counts[name] = counts.get(name, 0) + 1
# create a dictionary from initial list
# it doesn't matter how collisions are resolved
# the dictionary is required to process each name only once
init = dict(pairs)
res = []
# for each name:
# if count is > 1, print the count
# if count is 1, then print its date
for (name, date) in init.items():
if counts[name] > 1:
res.append(name + 'x' + str(counts[name]))
else:
res.append(name + '(' + date)
print(res)
</code></pre>