小编Jac*_*ent的帖子

将python中的字典值从str转换为嵌套字典中的int

我目前有一个嵌套字典,如下所示:

series = { 

'a': {'foo':'2', 'bar':'3', 'baz':'7', 'qux':'1'},
'b': {'foo':'6', 'bar':'4', 'baz':'3', 'qux':'0'},
'c': {'foo':'4', 'bar':'5', 'baz':'1', 'qux':'6'}
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试将字符串中的值转换为整数。

我试过这种方法:

newseries = dict((k,int(v)) for k,v in series.items())
Run Code Online (Sandbox Code Playgroud)

但我得到的只是一条错误消息:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict'
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

python dictionary

1
推荐指数
1
解决办法
3610
查看次数

标签 统计

dictionary ×1

python ×1