Có thể có bản sao trong từ điển python không?

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực thi chương trình nói trên

Trình chỉnh sửa mã Python

Có một cách khác để giải quyết giải pháp này?

Trước. Viết chương trình Python để lấy từ điển từ các trường của đối tượng.
Tiếp theo. Viết chương trình Python để kiểm tra từ điển có rỗng hay không.

Mức độ khó của bài tập này là gì?

Dễ dàng trung bình khó

Kiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource



Chia sẻ Hướng dẫn / Bài tập này trên. FacebookTwitter

con trăn. Lời khuyên trong ngày

Đảo ngược dữ liệu [Hàm đảo ngược & Phương pháp đảo ngược]

Hàm đảo ngược và phương thức đảo ngược chỉ có thể được sử dụng để đảo ngược các đối tượng trong Python. Nhưng có một sự khác biệt lớn giữa hai

Từ điển Python không cho phép lặp lại khóa. Tuy nhiên, chúng ta có thể sử dụng defaultdict để tìm cách giải quyết. Lớp này được định nghĩa trong mô-đun bộ sưu tập

Sử dụng danh sách làm nhà máy mặc định cho đối tượng defaultdict

>>> from collections import defaultdict
>>> d=defaultdict[list]

Dưới đây là danh sách các bộ dữ liệu có hai mục. Mục đầu tiên được tìm thấy được sử dụng nhiều lần. Danh sách này được chuyển đổi trong defaultdict

Trong khi xử lý từ điển, chúng ta có thể gặp tình huống khi có các giá trị trùng lặp trong từ điển trong khi rõ ràng các khóa vẫn là duy nhất. Trong bài viết này, chúng ta sẽ xem làm thế nào chúng ta có thể đạt được

Trao đổi khóa và giá trị

Chúng tôi trao đổi các khóa với các giá trị của từ điển và sau đó tiếp tục nối thêm các giá trị được liên kết với một khóa đã cho. Bằng cách này, các giá trị trùng lặp được xếp vào nhóm và chúng ta có thể thấy chúng trong từ điển mới kết quả

Thí dụ

Bản thử trực tiếp

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]

đầu ra

Chạy đoạn mã trên cho chúng ta kết quả sau -

Given Dictionary : {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}
New Dictionary: {5: ['Sun', 'Tue'], 3: ['Mon', 'Wed']}

với bộ

chúng tôi làm theo một cách tiếp cận tương tự ở đây. Ở đây, chúng tôi cũng tạo một từ điển mới từ từ điển hiện có bằng cách sử dụng chức năng thiết lập và thêm các khóa có giá trị trùng lặp. Cuối cùng, chúng tôi lọc ra các giá trị có độ dài lớn hơn 1 và đánh dấu chúng là trùng lặp

Đưa ra một từ điển, nhiệm vụ là tìm các khóa có giá trị trùng lặp. Hãy thảo luận về một vài phương pháp cho cùng một

Phương pháp #1. Sử dụng phương pháp Naive
Trước tiên, trong phương pháp này, chúng tôi chuyển đổi các giá trị trong từ điển thành các khóa bằng ánh xạ nghịch đảo, sau đó tìm các khóa trùng lặp




dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
15

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
16

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
18

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
19
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
20
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
21_______30
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
2
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
4
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
6
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
8
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
30
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
32
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
6
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
35

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
37

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
38
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
39____1150
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
152
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
153

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
155

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
156

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
157

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
158
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
20
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
160

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
162
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
163
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
164
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
165

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
166
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
167____1168
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
169

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
170

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
171
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
20
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
173____1162
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
175
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
164
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
177

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
178
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
179
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
180
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
181
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
2
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
183

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
185

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
38
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
39____1188
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
152
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
191

đầu ra.

Given Dictionary : {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}
New Dictionary: {5: ['Sun', 'Tue'], 3: ['Mon', 'Wed']}
7

 
Phương pháp #2. Sử dụng lật từ điển




dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
15

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
193

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
18

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
19
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
20
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
21_______30
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
2
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
4
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
6
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
8
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
30
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
32
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
6
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
35

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
37

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
38
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
39____1150
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
152
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
153

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
155

initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
05

initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
06
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
20
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
160

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
162
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
163
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
164
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
165

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
166
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
179
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
16
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
17
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
164
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
19

initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
20
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
21
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
20
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
23

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
166
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
25
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1

initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
20
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
28

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
185

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
38
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
39______333
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
152
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
36

đầu ra.

initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}

 
Phương pháp số 3. Sử dụng chuỗi và bộ

Giả sử bạn cần tìm các khóa có giá trị trùng lặp




dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
15

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
193

initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
39
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
40
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
41
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
42

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
18

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
19
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
20
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
21_______30
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
2
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
4
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
6
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
8
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
30
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
32
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
1
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
6
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
35

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
37

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
38
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
39____1150
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
152
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
153

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
155

initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
74

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
158
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
20
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
160

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
162
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
163
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
164
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
165

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
166
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
167____1168
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
169

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
171
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
20
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
168
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
91

initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
92______393____1162
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
175
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
164
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
177

initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
92______1179
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
180
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
181
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
2
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
303

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
17

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
185

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
38
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
39____1308
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
3
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
152
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
191

đầu ra.

dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
3

Thay vì




dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
171
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
20
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
168
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
91

initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
92______393____1162
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
175
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
164
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
177

initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
92______1179
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
180
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
181
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
2
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
303

Dùng cái này




dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
171
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
20
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
330
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
39
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
332
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
333
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
180
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
335
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
2
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}

print["Given Dictionary :", dictA]

k_v_exchanged = {}

for key, value in dictA.items[]:
   if value not in k_v_exchanged:
      k_v_exchanged[value] = [key]
   else:
      k_v_exchanged[value].append[key]

# Result
print["New Dictionary:", k_v_exchanged]
337

Từ điển Python có thể trùng lặp không?

Từ điển trong Python . Không cho phép sao chép khóa . Từ điển ánh xạ mỗi khóa thành một giá trị tương ứng, vì vậy sẽ không hợp lý khi ánh xạ một khóa cụ thể nhiều lần.

Từ điển có cho phép các thành viên trùng lặp không?

Từ điển là một bộ sưu tập không có thứ tự, có thể thay đổi và lập chỉ mục. Không có thành viên trùng lặp .

Từ điển trong Python có thể có cùng giá trị không?

Không, mỗi khóa trong từ điển phải là duy nhất . Bạn không thể có hai khóa có cùng giá trị. Cố gắng sử dụng lại cùng một khóa sẽ chỉ ghi đè lên giá trị được lưu trữ trước đó. Nếu một khóa cần lưu trữ nhiều giá trị thì giá trị được liên kết với khóa phải là một danh sách hoặc một từ điển khác.

Chủ Đề