Write a Python program to create a dictionary grouping a sequence of key-value pairs into a dictionary of lists. Use collections module.
- برمجة
- برمجة بايثون
- 2021-04-16
- salsabeelalhams99111801528
الأجوبة
from collections import defaultdict
def grouping_dictionary(l):
d = defaultdict(list)
for k, v in l:
d[k].append(v)
return d
colors = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
print("Original list:")
print(colors)
print("\nGrouping a sequence of key-value pairs into a dictionary of lists:")
print(grouping_dictionary(colors))أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال