将字典中的所有值转换为字符串
convert all the values in a dictionary to strings
假设我有一个以字符串和整数作为值的混合字典列表,我想将整数转换为字符串,考虑到列表,我应该如何做到这一点而不到处走动并逐个转换它们是流动的,长的,也可能将一些现有值更改为整数。
示例:
list = [{'a':'p', 'b':2, 'c':'k'},
{'a':'e', 'b':'f', 'c':5}]
现在,如果我尝试使用字符串打印列表的值,它会给我一个错误,如下所示。
示例:
for x in list:
print('the values of b are: '+x['b'])
输出:
TypeError: can only concatenate str (not "int") to str
Process finished with exit code 1
感谢任何帮助,谢谢!
解决方案
list = [{'a':'p', 'b':2, 'c':'k'},
{'a':'e', 'b':'f', 'c':5}]
for dicts in list:
for keys in dicts:
dicts[keys] = str(dicts[keys])
print('the values of b are: '+ dicts["b"])
试试这个
list = [{'a':'p', 'b':2, 'c':'k'},
{'a':'e', 'b':'f', 'c':5}]
for dicts in list:
for keys in dicts:
dicts[keys] = str(dicts[keys])
print(list)
如果要将字典值转换为字符串然后打印:
list = [{'a':'p', 'b':2, 'c':'k'}, {'a':'e', 'b':'f', 'c':5}]
list = [{str(j): str(i) for i, j in enumerate(d)} for d in list]
for x in list:
print("the values of b is: " + x['b'])
如果你只是想打印它们而不改变:
for x in list:
print(f"the values of b is: {x['b']}")
为什么不呢:
print(f"The value of b is: {x['b']}")
其他解决方案都一次性转换为字符串,但是,当您无法控制值随后是否改回时,这无济于事。
我建议将 dict 子类化为:
class StringDict(dict):
def __init__(self):
dict.__init__(self)
def __getitem__(self, y):
value_to_string = str(dict.__getitem__(self, y))
return value_to_string
def get(self, y):
value_to_string = str(dict.get(self, y))
return value_to_string
exampleDict = StringDict()
exampleDict["no_string"] = 123
print(exampleDict["no_string"])
123
print(type(exampleDict["no_string"]))
<class 'str'>
这样,值类型没有改变,但在访问时它会被动态转换为 String,保证它 returns 一个 String
也许这个:
list = [
{'a':'p', 'b':2, 'c':'k'},
{'a':'e', 'b':'f', 'c':5}
]
list = [{key: str(val) for key, val in dict.items()} for dict in list]
print(list)
假设我有一个以字符串和整数作为值的混合字典列表,我想将整数转换为字符串,考虑到列表,我应该如何做到这一点而不到处走动并逐个转换它们是流动的,长的,也可能将一些现有值更改为整数。
示例:
list = [{'a':'p', 'b':2, 'c':'k'},
{'a':'e', 'b':'f', 'c':5}]
现在,如果我尝试使用字符串打印列表的值,它会给我一个错误,如下所示。
示例:
for x in list:
print('the values of b are: '+x['b'])
输出:
TypeError: can only concatenate str (not "int") to str
Process finished with exit code 1
感谢任何帮助,谢谢!
解决方案
list = [{'a':'p', 'b':2, 'c':'k'},
{'a':'e', 'b':'f', 'c':5}]
for dicts in list:
for keys in dicts:
dicts[keys] = str(dicts[keys])
print('the values of b are: '+ dicts["b"])
试试这个
list = [{'a':'p', 'b':2, 'c':'k'},
{'a':'e', 'b':'f', 'c':5}]
for dicts in list:
for keys in dicts:
dicts[keys] = str(dicts[keys])
print(list)
如果要将字典值转换为字符串然后打印:
list = [{'a':'p', 'b':2, 'c':'k'}, {'a':'e', 'b':'f', 'c':5}]
list = [{str(j): str(i) for i, j in enumerate(d)} for d in list]
for x in list:
print("the values of b is: " + x['b'])
如果你只是想打印它们而不改变:
for x in list:
print(f"the values of b is: {x['b']}")
为什么不呢:
print(f"The value of b is: {x['b']}")
其他解决方案都一次性转换为字符串,但是,当您无法控制值随后是否改回时,这无济于事。
我建议将 dict 子类化为:
class StringDict(dict):
def __init__(self):
dict.__init__(self)
def __getitem__(self, y):
value_to_string = str(dict.__getitem__(self, y))
return value_to_string
def get(self, y):
value_to_string = str(dict.get(self, y))
return value_to_string
exampleDict = StringDict()
exampleDict["no_string"] = 123
print(exampleDict["no_string"])
123
print(type(exampleDict["no_string"]))
<class 'str'>
这样,值类型没有改变,但在访问时它会被动态转换为 String,保证它 returns 一个 String
也许这个:
list = [
{'a':'p', 'b':2, 'c':'k'},
{'a':'e', 'b':'f', 'c':5}
]
list = [{key: str(val) for key, val in dict.items()} for dict in list]
print(list)