Reputation: 597
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
I was expecting key to come as 2, 3, 4 and value to be a list. Something like below
{
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)]]
}
What is wrong with my code or am I missing something here?
Upvotes: 0
Views: 63
Reputation: 73450
Your print
statement is just messed up. Change it to:
print("Key: {}, Value: {}".format(key, value))
Your original
print("Key: {}", "Value: {}".format(key, value))
is printing the literal string "Key: {}"
(without any formatting), followed by the formatted string "Value: {}".format(key, value)
which uses the first argument key
to fill its only placeholder.
It is easy to get fooled here with the placeholder "{}"
looking like an empty dict
and all =)
Upvotes: 2