1

这个问题可能微不足道。如何添加从 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

但这对我来说是新的。我通常会展示更多内容,但我真的不知道如何编写代码。

4

4 回答 4

4

如果您有获奖者名单,例如:

>>> winners
['Boston Americans', 'World Series Not Played in 1904', 'New York', 'Chicago', 'Chicago', 'Chicago', 'Pittsburgh', 'Philadelphia', 'Philadelphia', 'Boston', 'Philadelphia', 'Boston', 'Boston', 'Boston']

您可以使用enumerate这些与数字相关联:

>>> list(enumerate(winners, 1903))
[(1903, 'Boston Americans'), (1904, 'World Series Not Played in 1904'), (1905, 'New York'), (1906, 'Chicago'), (1907, 'Chicago'), (1908, 'Chicago'), (1909, 'Pittsburgh'), (1910, 'Philadelphia'), (1911, 'Philadelphia'), (1912, 'Boston'), (1913, 'Philadelphia'), (1914, 'Boston'), (1915, 'Boston'), (1916, 'Boston')]

从这里你可以制作一个字典,或者一个字符串列表,或者其他任何东西:

>>> dict(enumerate(winners, 1903))
{1903: 'Boston Americans', 1904: 'World Series Not Played in 1904', 1905: 'New York', 1906: 'Chicago', 1907: 'Chicago', 1908: 'Chicago', 1909: 'Pittsburgh', 1910: 'Philadelphia', 1911: 'Philadelphia', 1912: 'Boston', 1913: 'Philadelphia', 1914: 'Boston', 1915: 'Boston', 1916: 'Boston'}
>>> ['{}:{}'.format(winner, year) for year, winner in enumerate(winners, 1903)]
['Boston Americans:1903', 'World Series Not Played in 1904:1904', 'New York:1905', 'Chicago:1906', 'Chicago:1907', 'Chicago:1908', 'Pittsburgh:1909', 'Philadelphia:1910', 'Philadelphia:1911', 'Boston:1912', 'Philadelphia:1913', 'Boston:1914', 'Boston:1915', 'Boston:1916']

您可以轻松地去掉“in YYYY”部分,但最好的方法取决于短语的可变性。

例如,如果您知道它是in YYYY,那么您可以使用类似

def strip_year(winner, year):
    in_year = ' in {}'.format(year)
    if winner.endswith(in_year):
        winner = winner[:-len(in_year)]
    return winner

然后使用字典理解(python >= 2.7):

>>> {year: strip_year(winner, year) for year, winner in enumerate(winners, 1903)}
{1903: 'Boston Americans', 1904: 'World Series Not Played', 1905: 'New York', 1906: 'Chicago', 1907: 'Chicago', 1908: 'Chicago', 1909: 'Pittsburgh', 1910: 'Philadelphia', 1911: 'Philadelphia', 1912: 'Boston', 1913: 'Philadelphia', 1914: 'Boston', 1915: 'Boston', 1916: 'Boston'}
于 2013-03-30T13:27:40.530 回答
1

认为:

my_dict = {"States" : ["Boston Americans", "World Series Not Played in 1904", "New York",  
           "Chicago", "Chicago", "Chicago"
           "Pittsburgh", "Philadelphia", "Philadelphia",
           "Boston", "Philadelphia", "Boston", "Boston","Boston"]}

那么这将做到这一点:

years = 1906
for key in my_dict.keys():
  year_list = []
  for year in my_dict[key][0].split(","):
    if re.search(start,year):
      year_list.append(year)
    else:
      year_list.append(year + ":" + years)
    years += 1
  my_dict[key] = year_list
于 2013-03-30T13:24:11.030 回答
1

类似的东西:

>>> a = ['a','b','c','d in 1906','e']
>>> b = range(1903,1903+len(a))
>>> b
[1903, 1904, 1905, 1906, 1907]
>>> zip(a,b)
[('a', 1903), ('b', 1904), ('c', 1905), ('d in 1906', 1906), ('e', 1907)]
>>> c = zip(a,b)
>>> d = [(i[0][:-7],i[1]) if i[0].endswith(str(i[1])) else (i[0],i[1]) for i in c]
>>> d
[('a', 1903), ('b', 1904), ('c', 1905), ('d ', 1906), ('e', 1907)]

之后你可以dict(d)用来获取字典

于 2013-03-30T13:26:37.267 回答
1

使用 Python 的列表推导并定义一个辅助函数,如果它们尚未出现,则将文本与年份连接起来。

您可以使用可选的第二个参数enumerate来指示起始值- 您的第一年。

def add_year_to(state, year):
    year = str(year)
    return state if state.endswith(year) else ':'.join((state, year))


states_with_years = [add_year_to(state, year) 
                     for year, state
                     in enumerate(states, 1903)]
于 2013-03-30T13:32:14.517 回答