Python – 字典键值列表组合
给定具有值作为列表的字典,提取所有可能的组合,包括交叉键和值。
Input : test_dict = {“Gfg” : [4, 5], “is” : [1, 2], “Best” : [9, 4]}
Output : {0: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 9]], 1: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 4]], 2: [[‘Gfg’, 4], [‘is’, 2], [‘Best’, 9]], 3: [[‘Gfg’, 4], [‘is’, 2], [‘Best’, 4]], 4: [[‘Gfg’, 5], [‘is’, 1], [‘Best’, 9]], 5: [[‘Gfg’, 5], [‘is’, 1], [‘Best’, 4]], 6: [[‘Gfg’, 5], [‘is’, 2], [‘Best’, 9]], 7: [[‘Gfg’, 5], [‘is’, 2], [‘Best’, 4]]}
Explanation : Prints all possible combination of key with values and cross values as well.
Input : test_dict = {“Gfg” : [4], “is” : [1], “Best” : [4]}
Output : {0: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 4]]}
Explanation : Prints all possible combination of key with values and cross values as well.
方法 #1:使用 product() + zip() + loop
上述功能的组合可以用来解决这个问题。在此,我们使用 product() 执行所有值的第一个键组合,并使用 zip() 和循环执行交叉键组合。
Python3
# Python3 code to demonstrate working of
# Dictionary Key Value lists combinations
# Using product() + zip() + loop
from itertools import product
# initializing dictionary
test_dict = {"Gfg" : [4, 5, 7],
"is" : [1, 2, 9],
"Best" : [9, 4, 2]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
temp = list(test_dict.keys())
res = dict()
cnt = 0
# making key-value combinations using product
for combs in product (*test_dict.values()):
# zip used to perform cross keys combinations.
res[cnt] = [[ele, cnt] for ele, cnt in zip(test_dict, combs)]
cnt += 1
# printing result
print("The computed combinations : " + str(res))
Python3
# Python3 code to demonstrate working of
# Dictionary Key Value lists combinations
# Using product() + loop
from itertools import product
# initializing dictionary
test_dict = {"Gfg" : [4, 5, 7],
"is" : [1, 2, 9],
"Best" : [9, 4, 2]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res = {}
for key, val in test_dict.items():
# for key-value combinations
res[key] = product([key], val)
# computing cross key combinations
res = product(*res.values())
# printing result
print("The computed combinations : " + str(list(res)))
The original dictionary is : {‘Gfg’: [4, 5, 7], ‘is’: [1, 2, 9], ‘Best’: [9, 4, 2]}
The computed combinations : {0: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 9]], 1: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 4]], 2: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 2]], 3: [[‘Gfg’, 4], [‘is’, 2], [‘Best’, 9]], 4: [[‘Gfg’, 4], [‘is’, 2], [‘Best’, 4]], 5: [[‘Gfg’, 4], [‘is’, 2], [‘Best’, 2]], 6: [[‘Gfg’, 4], [‘is’, 9], [‘Best’, 9]], 7: [[‘Gfg’, 4], [‘is’, 9], [‘Best’, 4]], 8: [[‘Gfg’, 4], [‘is’, 9], [‘Best’, 2]], 9: [[‘Gfg’, 5], [‘is’, 1], [‘Best’, 9]], 10: [[‘Gfg’, 5], [‘is’, 1], [‘Best’, 4]], 11: [[‘Gfg’, 5], [‘is’, 1], [‘Best’, 2]], 12: [[‘Gfg’, 5], [‘is’, 2], [‘Best’, 9]], 13: [[‘Gfg’, 5], [‘is’, 2], [‘Best’, 4]], 14: [[‘Gfg’, 5], [‘is’, 2], [‘Best’, 2]], 15: [[‘Gfg’, 5], [‘is’, 9], [‘Best’, 9]], 16: [[‘Gfg’, 5], [‘is’, 9], [‘Best’, 4]], 17: [[‘Gfg’, 5], [‘is’, 9], [‘Best’, 2]], 18: [[‘Gfg’, 7], [‘is’, 1], [‘Best’, 9]], 19: [[‘Gfg’, 7], [‘is’, 1], [‘Best’, 4]], 20: [[‘Gfg’, 7], [‘is’, 1], [‘Best’, 2]], 21: [[‘Gfg’, 7], [‘is’, 2], [‘Best’, 9]], 22: [[‘Gfg’, 7], [‘is’, 2], [‘Best’, 4]], 23: [[‘Gfg’, 7], [‘is’, 2], [‘Best’, 2]], 24: [[‘Gfg’, 7], [‘is’, 9], [‘Best’, 9]], 25: [[‘Gfg’, 7], [‘is’, 9], [‘Best’, 4]], 26: [[‘Gfg’, 7], [‘is’, 9], [‘Best’, 2]]}
方法 #2:使用 product() + 循环
上述功能的组合也可以用来解决这个问题。在此,我们使用 product() 执行执行内部和交叉键组合的任务。不同之处在于分组容器是元组而不是列表。
Python3
# Python3 code to demonstrate working of
# Dictionary Key Value lists combinations
# Using product() + loop
from itertools import product
# initializing dictionary
test_dict = {"Gfg" : [4, 5, 7],
"is" : [1, 2, 9],
"Best" : [9, 4, 2]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res = {}
for key, val in test_dict.items():
# for key-value combinations
res[key] = product([key], val)
# computing cross key combinations
res = product(*res.values())
# printing result
print("The computed combinations : " + str(list(res)))
The original dictionary is : {‘Gfg’: [4, 5, 7], ‘is’: [1, 2, 9], ‘Best’: [9, 4, 2]}
The computed combinations : [((‘Gfg’, 4), (‘is’, 1), (‘Best’, 9)), ((‘Gfg’, 4), (‘is’, 1), (‘Best’, 4)), ((‘Gfg’, 4), (‘is’, 1), (‘Best’, 2)), ((‘Gfg’, 4), (‘is’, 2), (‘Best’, 9)), ((‘Gfg’, 4), (‘is’, 2), (‘Best’, 4)), ((‘Gfg’, 4), (‘is’, 2), (‘Best’, 2)), ((‘Gfg’, 4), (‘is’, 9), (‘Best’, 9)), ((‘Gfg’, 4), (‘is’, 9), (‘Best’, 4)), ((‘Gfg’, 4), (‘is’, 9), (‘Best’, 2)), ((‘Gfg’, 5), (‘is’, 1), (‘Best’, 9)), ((‘Gfg’, 5), (‘is’, 1), (‘Best’, 4)), ((‘Gfg’, 5), (‘is’, 1), (‘Best’, 2)), ((‘Gfg’, 5), (‘is’, 2), (‘Best’, 9)), ((‘Gfg’, 5), (‘is’, 2), (‘Best’, 4)), ((‘Gfg’, 5), (‘is’, 2), (‘Best’, 2)), ((‘Gfg’, 5), (‘is’, 9), (‘Best’, 9)), ((‘Gfg’, 5), (‘is’, 9), (‘Best’, 4)), ((‘Gfg’, 5), (‘is’, 9), (‘Best’, 2)), ((‘Gfg’, 7), (‘is’, 1), (‘Best’, 9)), ((‘Gfg’, 7), (‘is’, 1), (‘Best’, 4)), ((‘Gfg’, 7), (‘is’, 1), (‘Best’, 2)), ((‘Gfg’, 7), (‘is’, 2), (‘Best’, 9)), ((‘Gfg’, 7), (‘is’, 2), (‘Best’, 4)), ((‘Gfg’, 7), (‘is’, 2), (‘Best’, 2)), ((‘Gfg’, 7), (‘is’, 9), (‘Best’, 9)), ((‘Gfg’, 7), (‘is’, 9), (‘Best’, 4)), ((‘Gfg’, 7), (‘is’, 9), (‘Best’, 2))]