【发布时间】:2013-03-30 13:12:26
【问题描述】:
这个问题可能微不足道。如何在不创建长 HUGE 年列表的情况下,将 1903 年以 2009 年结束的年份添加到列表中的 106 项 但是绕过一年?
例如:
States : Boston Americans, World Series Not Played in 1904, New York,
Chicago, Chicago, Chicago
Pittsburgh, Philadelphia, Philadelphia,
Boston, Philadelphia, Boston, Boston,Boston]`
到这里:
States : [Boston Americans:1903], [World Series Not Played:1904], [New York:1905],
[Chicago:1906],[Chicago:1907:],[Chicago:1908]....ect
虽然我知道您可以为列表中的每个项目添加一个数字计数
d = defaultdict(int)
for word in words.split():
d[word] += 1
我试过了:
d = {}
for i in new_list:
d[1903 + i] += 1 # I know this looks crazy but this is all I have
print(d)
我明白了
TypeError: 'function' object is not iterable
但这对我来说是新的。我通常会有更多展示,但我真的没有任何想法 如何对此进行编码。
【问题讨论】:
-
哎呀,让我改变一下。
-
唯一棘手的地方是删除“in YYYY”部分。它们是否都具有相同的格式,即以“in YYYY”结尾,或者类似于“1994--Canceled due to Strike”?
-
此时它并不像年份那么重要。感谢您的意见。
标签: python dictionary count add