Python 在collections.defaultdict(列表)上迭代

Python 在collections.defaultdict(列表)上迭代,python,python-3.x,Python,Python 3.x,我希望键是2,3,4,值是一个列表。 像下面这样 import itertools import collections def get_pairs(some_list, limit): min = 2 pair_dict = collections.defaultdict(list) for x in range(min, limit+1): pair_dict[x].append(list(itertools.combinations(some_l

我希望键是2,3,4,值是一个列表。 像下面这样

import itertools
import collections

def get_pairs(some_list, limit):
    min = 2
    pair_dict = collections.defaultdict(list)

    for x in range(min, limit+1):
        pair_dict[x].append(list(itertools.combinations(some_list, x)))

    return pair_dict

z = get_pairs([1, 2, 3, 4], 4)
for key, value in z.items():
    print("Key: {}", "Value: {}".format(key, value))

Output:
Key: {} Value: 2
Key: {} Value: 3
Key: {} Value: 4

我的代码有什么问题,或者我在这里遗漏了什么吗?

您的
打印语句简直是一团糟。将其更改为:

{
 2: [[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]], 
 3: [[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]], 
 4: [[(1, 2, 3, 4)]]
}
你的原创

print("Key: {}, Value: {}".format(key, value))
正在打印文本字符串
“Key:{}”
(无任何格式),然后是格式化字符串
“Value:{}”.format(Key,Value)
,该字符串使用第一个参数
Key
填充其唯一的占位符


在这里,占位符
“{}”
看起来像一个空的
dict
和all=)

你的
print
语句简直是一团糟。将其更改为:

{
 2: [[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]], 
 3: [[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]], 
 4: [[(1, 2, 3, 4)]]
}
你的原创

print("Key: {}, Value: {}".format(key, value))
正在打印文本字符串
“Key:{}”
(无任何格式),然后是格式化字符串
“Value:{}”.format(Key,Value)
,该字符串使用第一个参数
Key
填充其唯一的占位符

在这里,占位符
“{}”
看起来像一个空的
dict
和all=)很容易被愚弄