现在我有一个词典列表,例如
x = [{ '1':1, '2':2, '3':3, '4':'b'},
{ '1':1, '2':3, '4':'d'},
......]
我还有第二个字典列表,例如
y = [{'a':'a', 'b':'b'},
{'f':'f', 'h':'i', 'o':'p'},
{'a':'a', 'b':'b', 'c':'c', 'd':'d'},
.....]
第二个字典中的一些数据是初始列表的一个子部分,我如何使用第二个字典中的元素列表附加字典。 (例如,如果它在键b处具有值b。)
Result
x = [{ '1':1, '2':2, '3':3, 4:[{'a':'a', 'b':'b'},
{'a':'a', 'b':'b', 'c':'c', 'd':'d'}]},
{ '1':1, '2':3, '4':'d'}
......]
我创建了一个循环来查找所需的元素,但是我无法添加单个字典,但无法添加它们的列表。
我尝试使用x.append[1]['4'] = y[1]
或循环添加它们,但我无法让它工作。
非常感谢任何帮助。
我试图简化可能使它更加尴尬。编辑下面提供的词典样本以便清理。谢谢你的回答,我试图让他们工作,这是为了澄清。
balance = [{'Currency': 'ADA', 'Balance': 30},
{'Currency': 'SC', 'Balance': 200.2}
.....]
第二
history = [{'XC': 'ADA', 'Quantity': 13.2, 'PricePerUnit': 5},
{'XC': 'NAV', 'Quantity': 39.1, 'PricePerUnit': 25},
{'XC': 'ADA', 'Quantity': 63.49, 'PricePerUnit': 0.1}
....]
第一次迭代后,它将是
Result
balance = [{'Currency': 'ADA', 'Balance': 30, 'History':
[{'XC': 'ADA', 'Quantity': 13.2, 'PricePerUnit': 5},
{'XC': 'ADA', 'Quantity': 63.49, 'PricePerUnit': 0.1}]},
{'Currency': 'SC', 'Balance': 200.2}
.....]
对于第一个例子,它完成了:
x[1]['4'].append(y[2])
我试图完成的任务,没有迭代的答案是:
balance[0]['History'].append(history[0])
balance[0]['History'].append(history[2])
感谢Faibbus和Rakesh的解决方案!
答案 0 :(得分:0)
我不确定你想要完成什么,但看起来你想:
first_list = [{ 1:1, 2:2, 3:3, 4:'b'},
{1:1, 2:3, 4:'d'},]
second_list = [{'a':'a', 'b':'b'},
{'f':'f', 'h':'i', 'o':'p'},
{'a':'a', 'b':'b', 'c':'c', 'd':'d'},]
# Loop through all dictionaries:
for dictionary in first_list:
# Loop through all values
for key, value in dictionary.items():
if value == 'b':
# find all dictionaries matching you criteria
dicts = [d for d in second_list if d.get('b') == 'b']
if dicts:
# if there are dictionaries matching your criteria,
# replace the value with list of dictionaries
dictionary[key] = dicts
print first_list
将输出:
[{1: 1, 2: 2, 3: 3, 4:[{'a': 'a', 'b': 'b'},
{'a': 'a', 'c': 'c', 'b': 'b', 'd': 'd'}]},
{1: 1, 2: 3, 4: 'd'}]
如果您使用的是python3:
first_list = [{ 1:1, 2:2, 3:3, 4:'b'},
{1:1, 2:3, 4:'d'},]
second_list = [{'a':'a', 'b':'b'},
{'f':'f', 'h':'i', 'o':'p'},
{'a':'a', 'b':'b', 'c':'c', 'd':'d'},]
# Loop through all dictionaries:
for dictionary in first_list:
# Loop through all values
for key, value in list(dictionary.items()):
if value == 'b':
# find all dictionaries matching you criteria
dicts = [d for d in second_list if d.get('b') == 'b']
# Or you might want to remove the test on value, and
# replace 'b' by value in the previous line
if dicts:
# if there are dictionaries matching your criteria,
# replace the value with list of dictionaries
dictionary[key] = dicts
print (first_list)
要使用最新的编辑,您可以执行以下操作:
balance = [{'Currency': 'ADA', 'Balance': 30},
{'Currency': 'SC', 'Balance': 200.2}
]
history = [{'XC': 'ADA', 'Quantity': 13.2, 'PricePerUnit': 5},
{'XC': 'NAV', 'Quantity': 39.1, 'PricePerUnit': 25},
{'XC': 'ADA', 'Quantity': 63.49, 'PricePerUnit': 0.1}
]
# reorganize history:
new_history = {}
for record in history:
if not record['XC'] in new_history:
new_history[record['XC']] = []
new_history[record['XC']].append(record)
# Update balance to include history:
for account in balance:
if account['Currency'] in new_history:
account['history'] = new_history[account['Currency']]
print(balance)
导致
[{'Currency': 'ADA', 'Balance': 30, 'history': [{'XC': 'ADA', 'PricePerUnit': 5, 'Quantity': 13.2},
{'XC': 'ADA', 'PricePerUnit': 0.1, 'Quantity': 63.49}]},
{'Currency': 'SC', 'Balance': 200.2}]
答案 1 :(得分:0)
A = [{ 1:1, 2:2, 3:3, 4:'b'}, {1:1, 2:3, 4:'d'}]
B =[{'a':'a', 'b':'b'},
{'f':'f', 'h':'i', 'o':'p'},
{'a':'a', 'b':'b', 'c':'c', 'd':'d'}]
for i in A:
for k,v in i.iteritems():
for j in B:
if v in j.values():
if not isinstance(i[k], list):
i[k] = [j]
else:
i[k].append(j)
print A
<强>结果:强>
[{1: 1, 2: 2, 3: 3, 4: [{'a': 'a', 'b': 'b'}, {'a': 'a', 'c': 'c', 'b': 'b', 'd': 'd'}]}, {1: 1, 2: 3, 4: [{'a': 'a', 'c': 'c', 'b': 'b', 'd': 'd'}]}]