2013-03-30 52 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)] 
相關問題