为什么 itertools.product 不按顺序从字典中 return 键和值?请帮助

Why itertools.product dosen't return key and value from a dictionary in sequence? Help pls

我的情况如下。 像这样的嵌套循环:

d = [{'val1': 1, 'val2':2}, {'val3': 3, 'val4':4}, {'val5': 5, 'val6':6}, {'val7': 7, 'val8':8}, {'val9': 9, 'val10':10}]

num = list(range(len(d))
new_list = []
for i in num:
  for k, v in d[i].items():
    new_list.append([k ,v])

结果将是这样的:

[['val1', 1],
 ['val2', 2],
 ['val3', 3],
 ['val4', 4],
 ['val5', 5],
 ['val6', 6],
 ['val7', 7],
 ['val8', 8],
 ['val9', 9],
 ['val10', 10]]

Bu 需要找到一种方法来总结这些循环,要么使用像 Map 和 Filter 这样的函数,要么使用我读到的 Itertools.product 将嵌套循环变成一个循环。所以我做了这个代码:

new_list = []
for i, (k,v) in itertools.product(num, d[i].items()):
    new_list.append([k ,v])

结果是这样的:

    [['val9', 9],
 ['val10', 10],
 ['val9', 9],
 ['val10', 10],
 ['val9', 9],
 ['val10', 10],
 ['val9', 9],
 ['val10', 10],
 ['val9', 9],
 ['val10', 10]]

所以我想知道 itertools.product 是否有任何方法可以产生与带有嵌套循环的代码相同的结果,即输出是按顺序排列的,而不仅仅是重复最后两个值。 如果有mapfilter的解决方案,也欢迎

更新

@BrokenBenchmark 我的问题有点复杂,你能帮我吗? 问题是这样的:

d = [{'val1': 1, 'val2':2}, {'val3': 3, 'val4':4}, {'val5': 5, 'val6':6}, {'val7': 7, 'val8':8}, {'val9': 9, 'val10':10}]  
d2 = [{'val1': 1, 'val2':2}, {'val3': 3, 'val4':4}, {'val5': 5, 'val6':6}, {'val7': 7, 'val8':8}, {'val9': 9, 'val10':10}]  
num = list(range(len(d))
new_list = []  
for i in num:      
    for k_i, v_i in d[i].items():
          for j in num:              
              for k_j, v_j in d2[j].items():                  
                  new_list.append([k_i, k_j])

你有解决办法吗?这可以代替这个嵌套循环来加速代码。

我需要的输出是包含字典 1 中的键以及字典 2 中的所有键的列表(我需要将字典 1 中的每个键与字典 2 中的每个键进行比较)。输出应该是这样的:

[['Key1_dict1', 'Key1_dict2'],
['Key1_dict1', 'Key2_dict2'],
['Key1_dict1', 'Key3_dict2'],
['Key1_dict1', 'Key4_dict2'],
['Key1_dict1', 'Key5_dict2'],
['Key1_dict1', 'Key6_dict2'],
['Key1_dict1', 'Key7_dict2'],
['Key1_dict1', 'Key8_dict2'],
['Key1_dict1', 'Key9_dict2'],
['Key1_dict1', 'Key10_dict2'],
['Key2_dict1', 'Key1_dict2'],
['Key2_dict1', 'Key2_dict2'],
['Key2_dict1', 'Key3_dict2'],
['Key2_dict1', 'Key4_dict2'],
['Key2_dict1', 'Key5_dict2'],
['Key2_dict1', 'Key6_dict2'],
['Key2_dict1', 'Key7_dict2'],
['Key2_dict1', 'Key8_dict2'],
['Key2_dict1', 'Key9_dict2'],
['Key2_dict1', 'Key10_dict2']]

以此类推

如果不想使用嵌套的 for 循环,可以使用 .extend() 将字典的 key-value 对附加到列表中,并使用 map().items() 中的每个元素转换为列表。

itertools.product() 可以减少 for 循环中的嵌套,例如,当您需要遍历两个列表中的每对元素时。它不是删除所有嵌套for循环的灵丹妙药。

data = [{'val1': 1, 'val2':2}, {'val3': 3, 'val4':4}, {'val5': 5, 'val6':6}, {'val7': 7, 'val8':8}, {'val9': 9, 'val10':10}]

result = []
for dictionary in data:
    result.extend(map(list, dictionary.items()))    

print(result)

解决方案 #1:

        d = [{'val1': 1, 'val2':2}, {'val3': 3, 'val4':4}, {'val5': 5, 'val6':6}, {'val7': 7, 'val8':8}, {'val9': 9, 'val10':10}]  
        d2 = [{'val1': 1, 'val2':2}, {'val3': 3, 'val4':4}, {'val5': 5, 'val6':6}, {'val7': 7, 'val8':8}, {'val9': 9, 'val10':10}]

        new_list = [[k_i, k_j] for i in range(len(d)) for k_i, v_i in d[i].items() for j in range(len(d2)) for k_j, v_j in d2[j].items()]

输出:

[['val1', 'val1'], ['val1', 'val2'], ['val1', 'val3'], ['val1', 'val4'], ['val1', 'val5'], ['val1', 'val6'], ['val1', 'val7'], ['val1', 'val8'], ['val1', 'val9'], ['val1', 'val10'], ['val2', 'val1'], ['val2', 'val2'], ['val2', 'val3'], ['val2', 'val4'], ['val2', 'val5'], ['val2', 'val6'], ['val2', 'val7'], ['val2', 'val8'], ['val2', 'val9'], ['val2', 'val10'], ['val3', 'val1'], ['val3', 'val2'], ['val3', 'val3'], ['val3', 'val4'], ['val3', 'val5'], ['val3', 'val6'], ['val3', 'val7'], ['val3', 'val8'], ['val3', 'val9'], ['val3', 'val10'], ['val4', 'val1'], ['val4', 'val2'], ['val4', 'val3'], ['val4', 'val4'], ['val4', 'val5'], ['val4', 'val6'], ['val4', 'val7'], ['val4', 'val8'], ['val4', 'val9'], ['val4', 'val10'], ['val5', 'val1'], ['val5', 'val2'], ['val5', 'val3'], ['val5', 'val4'], ['val5', 'val5'], ['val5', 'val6'], ['val5', 'val7'], ['val5', 'val8'], ['val5', 'val9'], ['val5', 'val10'], ['val6', 'val1'], ['val6', 'val2'], ['val6', 'val3'], ['val6', 'val4'], ['val6', 'val5'], ['va...

解决方案#2:

        k, k2 = chain(*[m.keys() for m in d]), chain(*[m.keys() for m in d2])
        new_list_of_tuples = list(product(k, k2)) # if tuples are OK as key pairs
        new_list = [[*x] for x in product(k, k2)] # if you need lists as key pairs