Thomas Jones
Thomas Jones

Reputation: 365

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?

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.

Upvotes: 1

Views: 654

Answers (4)

DSM
DSM

Reputation: 353329

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:

>>> 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.

For example, if you know it's in YYYY, then you could use something like

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

>>> {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'}

Upvotes: 4

Adam
Adam

Reputation: 15803

Use Python's list comprehensions and define a helper function that joins the text with the years if they are not yet present.

You can use the optional second parameter of enumerate to indicate the start value – your first year.

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

Upvotes: 1

ennuikiller
ennuikiller

Reputation: 46965

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

Upvotes: 1

lenik
lenik

Reputation: 23556

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

Upvotes: 1

Related Questions