繁体   English   中英

在字典中将字符串更改为整数。 Python

[英]changing strings to integers in a dictionary. python

这是我的代码,它尝试将 dict 中的字符串更改为整数:

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'的第一行,但不知道如何将数字转换为整数。

那不是字典,而是列表列表。 就像是:

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

应该管用。

暂无
暂无

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

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