简体   繁体   English

我如何从 dict 值和元组列表中获取每个元素并从中生成一个元组

[英]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.

相关问题 如何从嵌套的元组列表中获取每个元组的第一个元素? - How do I get the first element of every tuple from a nested list of tuples? 如何基于元组列表合并 2 个数据帧,其中每个元组都包含来自每个数据帧的相关键? - How can I merge 2 dataframes based on a list of tuples, where every tuple holds the relevant key from every dataframe? 每次元素更改时如何获取新列表(元素是元组列表中每个元组的特定索引) - How to get a new list every time an element changes (the element being a certain index for every tuple in a list of tuples) 优雅的方法从元组列表中提取元组,具有最小的元素值 - Elegant way to extract a tuple from list of tuples with minimum value of element 如何从数据框/矩阵中获取值到列表的元组中 - How can I get value from dataframe/matrix into tuple of list 如何从元组列表中获取价值并创建新列表 - how to get value from a list of tuples and make a new list 从元组列表中,获取最接近给定值的元组 - from list of tuples, get tuple closest to a given value 如何从元组列表中创建列表? - How can I make a list from a list of tuples ? 如何获得元组和元组列表之间的最近距离? - How can i get the closest distance between a tuple and a list of tuples? 如何从一个元组中提取一个值? - How does one pull a value from within a tuple of tuples?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM