这是我的代码,试图将字典中的字符串更改为整数:
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'
问题字典是这样的:
[['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']]
我试图将所有数字都作为整数删除'0 to 4 years'
。我成功删除了第一行'0 to 4 years'
,但不知道如何将数字转换为整数。
答案 0 :(得分:0)
那不是字典,而是列表的列表。像这样:
[([item[0]] + [int(x) for x in item[1:]]) for item in data]
应该工作。