我有一个嵌套的json,如下所示:
[
{
'Name':'Anders',
'Type':'Hunter',
'Race':'Cyborg',
},
{
'Name':'Karin',
'Type':'Titan',
'Race':'Human',
},
{
'Name':'Jenny',
'Type':'Warlock',
'Race':'Marsian',
},
]
我有一个看起来像这样的字符串:
['1', 'Ham', 'Spam', 'Bacon', '1', 'Ham', 'Cucumber', 'Tomato', '1', 'Wood', 'Potato', 'Herring']
我想将字符串插入json,并带有值的名称。字符串中的每四个都是json中的每个人。
最终结果应如下所示:
[
{
'Name':'Anders',
'Type':'Hunter',
'Race':'Cyborg',
'string1':'1',
'Food_1':'Ham',
'Food_2':'Spam',
'Food_3':'Bacon',
},
{
'Name':'Karin',
'Type':'Titan',
'Race':'Human',
'string1':'1',
'Food_1':'Ham',
'Food_2':'Cucumber',
'Food_3':'Tomato',
},
{
'Name':'Jenny',
'Type':'Warlock',
'Race':'Marsian',
'string1':'1',
'Food_1':'Wood',
'Food_2':'Potato',
'Food_3':'Herring',
},
]
我尝试了一些迭代,但总是失败:/
希望你能帮助我!
答案 0 :(得分:1)
我假设你的数据结构是python dicts和list,而不是包含json-text的字符串。
首先,将您的平面事项列表插入到每个包含4个项目的块的列表中。您可以阅读其他一些方法here。
new_things = ['1', 'Ham', 'Spam', 'Bacon', '1', 'Ham', 'Cucumber', 'Tomato', '1', 'Wood', 'Potato', 'Herring']
chunks = [new_things[i:i+4] for i in range(0, len(new_things), 4)]
导致:
[['1', 'Ham', 'Spam', 'Bacon'], ['1', 'Ham', 'Cucumber', 'Tomato'], ['1', 'Wood', 'Potato', 'Herring']]
然后,同时迭代这两个集合。您可以使用zip:
for entry, new_stuff in zip(existing_data, chunks):
entry["string1"] = new_stuff[0]
entry["Food_1"] = new_stuff[1]
entry["Food_2"] = new_stuff[2]
entry["Food_3"] = new_stuff[3]
之后,您的收藏应该与预期的一样。
答案 1 :(得分:0)
替代答案1:
json_list = [
{
'Name':'Anders',
'Type':'Hunter',
'Race':'Cyborg',
},
{
'Name':'Karin',
'Type':'Titan',
'Race':'Human',
},
{
'Name':'Jenny',
'Type':'Warlock',
'Race':'Marsian',
},]
foods = ['1', 'Ham', 'Spam', 'Bacon', '1', 'Ham', 'Cucumber', 'Tomato', '1', 'Wood', 'Potato', 'Herring']
for _ in json_list:
_.update({'string1': foods[0], 'food_1': foods[1], 'food_2': foods[2], 'food_3': foods[3]})
foods = foods[4:]
json_list
结果
[{'Race': 'Cyborg', 'food_3': 'Bacon', 'string1': '1', 'food_2': 'Spam', 'Name': 'Anders', 'Type': 'Hunter', 'food_1': 'Ham'}, {'Race': 'Human', 'food_3': 'Tomato', 'string1': '1', 'food_2': 'Cucumber', 'Name': 'Karin', 'Type': 'Titan', 'food_1': 'Ham'}, {'Race': 'Marsian', 'food_3': 'Herring', 'string1': '1', 'food_2': 'Potato', 'Name': 'Jenny', 'Type': 'Warlock', 'food_1': 'Wood'}]
答案 2 :(得分:0)
执行此操作的紧凑方法是使用iter
和zip
函数将新数据分解为块。
import json
data = [
{
'Name':'Anders',
'Type':'Hunter',
'Race':'Cyborg',
},
{
'Name':'Karin',
'Type':'Titan',
'Race':'Human',
},
{
'Name':'Jenny',
'Type':'Warlock',
'Race':'Marsian',
},
]
keys = ['string1', 'Food_1', 'Food_2', 'Food_3']
new_data = [
'1', 'Ham', 'Spam', 'Bacon',
'1', 'Ham', 'Cucumber', 'Tomato',
'1', 'Wood', 'Potato', 'Herring',
]
# Create an iterator that yields chunks of length 4 from new_data
chunks = zip(*[iter(new_data)] * 4)
# Update the dicts in data with the new data
for d, t in zip(data, chunks):
d.update(zip(keys, t))
# Convert to JSON
json_data = json.dumps(data, indent=4)
print(json_data)
<强>输出强>
[
{
"Name": "Anders",
"Type": "Hunter",
"Race": "Cyborg",
"string1": "1",
"Food_1": "Ham",
"Food_2": "Spam",
"Food_3": "Bacon"
},
{
"Name": "Karin",
"Type": "Titan",
"Race": "Human",
"string1": "1",
"Food_1": "Ham",
"Food_2": "Cucumber",
"Food_3": "Tomato"
},
{
"Name": "Jenny",
"Type": "Warlock",
"Race": "Marsian",
"string1": "1",
"Food_1": "Wood",
"Food_2": "Potato",
"Food_3": "Herring"
}
]
我们也可以把&#34; chunkifying&#34;进入主for
循环:
for d, t in zip(data, zip(*[iter(new_data)] * 4)):
d.update(zip(keys, t))
但我认为以前的版本更具可读性。