Professional Documents
Culture Documents
Phan Thông 16141292 Lab3
Phan Thông 16141292 Lab3
Bước 3: Tạo ra từ điển của các danh sách (giống như những kết quả của từ điển)
Từ điển Python là cấu trúc dữ liệu mà bạn nên sử dụng cho cái “Pet Image filenames” (như
là những từ khóa) và “danh sách“ mà nó chứa filenames kết hợp với nhãn (như là kết quả).
Đoạn code này sẽ giúp bạn sử dụng từ điển python
# Creates empty dictionary named results_dic
results_dic = dict()
# Determines number of items in dictionary
items_in_dic = len(results_dic)
print("\nEmpty Dictionary results_dic - n items=", items_in_dic)
# Adds new key-value pairs to dictionary ONLY when key doesn't already exist. This dictionary's value is
# a List that contains only one item - the pet image label
filenames = ["beagle_0239.jpg", "Boston_terrier_02259.jpg"]
pet_labels = ["beagle", "boston terrier"]
for idx in range(0, len(filenames), 1):
if filenames[idx] not in results_dic:
results_dic[filenames[idx]] = [pet_labels[idx]]
else:
print("** Warning: Key=", filenames[idx],
"already exists in results_dic with value =",
results_dic[filenames[idx]])
#Iterating through a dictionary printing all keys & their associated values
print("\nPrinting all key-value pairs in dictionary results_dic:")
for key in results_dic:
print("Filename=", key, " Pet Label=", results_dic[key][0])