简体   繁体   中英

changing strings to integers in a dictionary. python

This is my code, which attempts to change strings in a dict to integers:

data1 = dict((k, int(v)) for k, v in data.items())
Traceback (most recent call last):
  File "/Users/tanwirshirzai/Desktop/starter files/a6q1_starter.py", line 18, in <module>
    data1=dict((k, int(v)) for k, v in data.items())
AttributeError: 'list' object has no attribute 'items'

The dictionary in question is this:

[['0 to 4 years', '1918924', '1921123', '1924604', '1942022', '1953040'], 
 ['5 to 9 years', '1882687', '1918323', '1952041', '1985144', '2003143'], 
 ['10 to 14 years', '1868495', '1865818', '1864760', '1886340', '1920898']]

I am trying to get all the numbers as integers deleting the '0 to 4 years' . I succeeded in deleting the first row which is '0 to 4 years' but don't know how to convert the numbers to integers.

That's not a dictionary, it's a list of lists. Something like:

[([item[0]] + [int(x) for x in item[1:]]) for item in data]

should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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