如何将字典中的字符串值转换为 int/float 数据类型?

2022-01-14 00:00:00 python list dictionary string int

问题描述

我有一个字典列表如下:

I have a list of dictionaries as follows:

list = [ { 'a':'1' , 'b':'2' , 'c':'3' }, { 'd':'4' , 'e':'5' , 'f':'6' } ]

如何将列表中每个字典的值转换为 int/float?

How do I convert the values of each dictionary inside the list to int/float?

这样就变成了:

list = [ { 'a':1 , 'b':2 , 'c':3 }, { 'd':4 , 'e':5 , 'f':6 } ]

谢谢.


解决方案

for sub in the_list:
    for key in sub:
        sub[key] = int(sub[key])

将其转换为 int 而不是字符串.

Gives it a casting as an int instead of as a string.

相关文章