简体   繁体   English

向列表或词典中的每个项目添加年份

[英]Add a Year to each item in list or Dictionary

This question maybe trivial. 这个问题也许微不足道。 How do I add a year starting with 1903 ending with 2009 to 106 items on a list without creating a long HUGE list of years But bypass ones with a year? 如何添加,而不与去年创造的年,但旁路那些很长的巨大的名单一年开始,1903年至2009年106项目的清单上结束?

For Example: 例如:

  States : Boston Americans, World Series Not Played in 1904, New York,  
           Chicago, Chicago, Chicago
           Pittsburgh, Philadelphia, Philadelphia,
           Boston, Philadelphia, Boston, Boston,Boston]`

To this: 对此:

  States : [Boston Americans:1903], [World Series Not Played:1904], [New York:1905],  
           [Chicago:1906],[Chicago:1907:],[Chicago:1908]....ect

While I know you can add a number count to each item on list 据我所知,您可以为列表中的每个项目添加数字计数

 d = defaultdict(int)
 for word in words.split():
     d[word] += 1

I tried: 我试过了:

 d = {}
 for i in new_list:
     d[1903 + i] += 1 # I know this looks crazy but this is all I have
     print(d)

I get 我懂了

TypeError: 'function' object is not iterable

But this is new to me. 但这对我来说是新的。 I would normally have more to show but I really don't have any Idea how to code this at all. 通常,我会展示更多内容,但是我根本不知道该如何编写代码。

If you have a list of winners, like: 如果您有获奖者名单,例如:

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

You can use enumerate to associate these with numbers: 您可以使用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')]

And from this you can make a dict, or a list of strings, or whatever: 然后,您可以编写字典,字符串列表或其他任何内容:

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

You can strip the "in YYYY" part easily enough, but the best way to do that depends upon how variable the phrases are. 您可以轻松地剥离“ in YYYY”部分,但是最好的方法取决于短语的可变性。

For example, if you know it's in YYYY , then you could use something like 例如,如果您知道它in YYYY ,则可以使用类似

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

and then use a dictionary comprehension (python >= 2.7): 然后使用字典理解(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'}

suppose: 假设:

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

then this will do it: 然后这样做:

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

something like that: 像这样的东西:

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

and you may use dict(d) to get a dictionary afterwards 然后您可以使用dict(d)获得字典

Use Python's list comprehensions and define a helper function that joins the text with the years if they are not yet present. 使用Python的列表推导并定义一个辅助函数,该函数将文本和年份(如果还不存在)连接在一起。

You can use the optional second parameter of enumerate to indicate the start value – your first year. 您可以使用可选的第二个参数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)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM