[英]How i can get every element from dict value and list of tuples and make one tuple from it
i Have a dict with next structure:我有一个具有下一个结构的字典:
a = {'test_1': 1, 'test_2': 2}
and i have a second dict with list of tuples like value for key 'size_measures_multi':我有第二个字典,其中包含元组列表,例如键“size_measures_multi”的值:
b = [{'row_n': 1, 'row_section': None, 'name': 'name', 'value_1': 'qwe', 'key_1': 'test', 'value_2': [('1', 'some_value', 'some_value'), ('2', 'some_value', 'some_value')]}, {'row_n': 2, 'row_section': None, 'some_value_1': 'some_name, 'some_value_2': 'test', 'value_3': 'text', 'value_4': [('167,86', 'cm', 'netto'), ('1', 'cm', 'netto')]}]
I need get every value from dict 'a' and every first element from list of tuples from list of dict 'b' by size_measures_multi like a key and make one tuple from it.我需要从 dict 'a' 中获取每个值,并通过 size_measures_multi 从 dict 'b' 列表中获取元组列表中的每个第一个元素,就像一个键一样,并从中创建一个元组。 Also, i need appen it to some empty list.
另外,我需要将它附加到一些空列表中。
Result wich I expect结果出乎我的意料
result=[(136873521, '1', '2'), (136873857, '167,86', '1')]
结果=[(136873521, '1', '2'), (136873857, '167,86', '1')]
You probably don't need to use for row in b
since you're already loop through b
using element
already.您可能不需要
for row in b
因为您已经使用element
遍历b
。 You just need to remove that and the list []
brackets in after append
function and it should work properly.您只需要删除它并在
append
function 之后删除列表[]
括号,它应该可以正常工作。
a = {'FORD#BE8Z9278-A': 136873521, 'FORD#CN1G-6L713-BC': 136873857}
b = [
{'row_n': 1, 'row_section': None, 'nomenclature_name': 'name', 'article': 'BE8Z9278-A', 'TM': 'FORD',
'size_measures_multi': [('1', 'cm', 'netto'), ('2', 'cm', 'netto')]
},
{'row_n': 2, 'row_section': None, 'nomenclature_name': 'some_name', 'article': 'CN1G-6L713-BC', 'TM': 'FORD',
'size_measures_multi': [('167,86', 'cm', 'netto'), ('1', 'cm', 'netto')]
}
]
result = []
for element in b:
new_key = "{}#{}".format(element['TM'], element['article']) # Here i get 'FORD#BE8Z9278-A' for example
result.append((a[new_key],) + tuple(item[0] for item in element['size_measures_multi']))
print(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.