只要键为“ current_values ”,我想用与 integers 相同的值替换这些值(格式为字符串)。
d = {'id': '10', 'datastreams': [{'current_value': '5'}, {'current_value': '4'}]}
所需的输出:
d = {'id': '10', 'datastreams': [{'current_value': 5}, {'current_value': 4}]}
答案 0 :(得分:2)
一种通用方法(假设您事先不知道dict的哪个键指向列表)将遍历该dict并检查其值的类型,然后在需要时再次迭代到每个值。
在您的情况下,您的字典可能包含一个字典列表作为值,因此足以检查一个值是否为list类型,如果是,则遍历该列表并更改所需的字典。
可以使用以下函数递归完成该操作:
def f(d):
for k,v in d.items():
if k == 'current_value':
d[k] = int(v)
elif type(v) is list:
for item in v:
if type(item) is dict:
f(item)
>>> d = {'id': '10', 'datastreams': [{'current_value': '5'}, {'current_value': '4'}]}
>>> f(d)
>>> d
{'id': '10', 'datastreams': [{'current_value': 5}, {'current_value': 4}]}
答案 1 :(得分:1)
可以通过列表理解来完成:
d['datastreams'] = [{'current_value': int(ds['current_value'])} if ('current_value' in ds) else ds for ds in d['datastreams']]
答案 2 :(得分:1)
d = {'id': '10', 'datastreams': [{'current_value': '5'}, {'current_value': '4'}]}
for elem in d['datastreams']: # for each elem in the list datastreams
for k,v in elem.items(): # for key,val in the elem of the list
if 'current_value' in k: # if current_value is in the key
elem[k] = int(v) # Cast it to int
print(d)
输出:
{'id': '10', 'datastreams': [{'current_value': 5}, {'current_value': 4}]}
答案 3 :(得分:1)
下面的代码片段替换字典中的值(的子字符串)。它适用于嵌套的json结构,并可以处理json,列表和字符串类型。您可以根据需要添加其他类型。
def dict_replace_value(d, old, new):
x = {}
for k, v in d.items():
if isinstance(v, dict):
v = dict_replace_value(v, old, new)
elif isinstance(v, list):
v = list_replace_value(v, old, new)
elif isinstance(v, str):
v = v.replace(old, new)
x[k] = v
return x
def list_replace_value(l, old, new):
x = []
for e in l:
if isinstance(e, list):
e = list_replace_value(e, old, new)
elif isinstance(e, dict):
e = dict_replace_value(e, old, new)
elif isinstance(e, str):
e = e.replace(old, new)
x.append(e)
return x
# See input and output below
b = dict_replace_value(a, 'string', 'something')
输入:
a = {
'key1': 'a string',
'key2': 'another string',
'key3': [
'a string',
'another string',
[1, 2, 3],
{
'key1': 'a string',
'key2': 'another string'
}
],
'key4': {
'key1': 'a string',
'key2': 'another string',
'key3': [
'a string',
'another string',
500,
1000
]
},
'key5': {
'key1': [
{
'key1': 'a string'
}
]
}
}
输出:
{
"key1":"a something",
"key2":"another something",
"key3":[
"a something",
"another something",
[
1,
2,
3
],
{
"key1":"a something",
"key2":"another something"
}
],
"key4":{
"key1":"a something",
"key2":"another something",
"key3":[
"a something",
"another something",
500,
1000
]
},
"key5":{
"key1":[
{
"key1":"a something"
}
]
}
}
答案 4 :(得分:0)
您可以使用ast.literal_eval通过d ['datastreams']列表中的items
键来评估current_value
的基础值。然后使用int
来检查类型是否为isinstance
。最后,将此类值转换为int
。
import ast
d = {'id': '10', 'datastreams': [{'current_value': '5'}, {'current_value': '4'}]}
for i in d['datastreams']:
for k,v in i.items():
if 'current_value' in k and isinstance(ast.literal_eval(v),int):
i[k] = int(v)
#Output:
print(d)
{'id': '10', 'datastreams': [{'current_value': 5}, {'current_value': 4}]}
答案 5 :(得分:0)
您可以使用此方法 它将循环检查列表中的current_value并通过将值传递给int()函数将其更改为整数:
react-animate-on-scroll