2013-03-30 55 views
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

但是,这是新的我。我通常会有更多的展示,但我真的没有任何想法 如何编码这一点。

+0

oops让我改变这一点。 –

+0

唯一棘手的问题是删除“YYYY”部分。它们是否都具有相同的格式,即以“YYYY”结尾,还是有些类似于“1994--由于Strike而取消”? – DSM

+0

在这一点上,不像年份那么重要。感谢您的输入。 –

回答

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'] 

可以剥“在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 

,然后使用字典解析(蟒蛇> = 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'} 
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 
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)得到一本字典之后

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)] 
相关问题