给列表或字典中的每一项添加一年

2024-09-30 05:20:30 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题也许微不足道。如何将从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]`

为此:

^{pr2}$

我知道你可以给单子上的每一项加上一个数字

 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

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


Tags: in列表forworldis时间thisboston
3条回答

假设:

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

这样就可以做到:

^{pr2}$

如果你有一份获奖名单,比如:

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

您可以使用enumerate将这些与数字关联起来:

^{pr2}$

从这里你可以做一个dict,或者一个字符串列表,或者别的什么:

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

你可以很容易地去掉“inyyyy”部分,但最好的方法取决于短语的变化程度。在

例如,如果您知道它是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'}

像这样:

>>> 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)来获取字典

相关问题 更多 >

    热门问题