เมื่อจำเป็นต้องแปลงรายการเป็นพจนานุกรมค่าดัชนี ระบบจะใช้ "ระบุ" และการวนซ้ำอย่างง่าย
ตัวอย่าง
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
my_list = [32, 0, 11, 99, 223, 51, 67, 28, 12, 94, 89]
print("The list is :")
print(my_list)
my_list.sort(reverse=True)
print("The sorted list is ")
print(my_list)
index, value = "index", "values"
my_result = {index : [], value : []}
for id, vl in enumerate(my_list):
my_result[index].append(id)
my_result[value].append(vl)
print("The result is :")
print(my_result) ผลลัพธ์
The list is :
[32, 0, 11, 99, 223, 51, 67, 28, 12, 94, 89]
The sorted list is
[223, 99, 94, 89, 67, 51, 32, 28, 12, 11, 0]
The result is :
{'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'values': [223, 99, 94, 89, 67, 51, 32, 28, 12, 11, 0]} คำอธิบาย
-
รายการจำนวนเต็มถูกกำหนดและแสดงบนคอนโซล
-
รายการจะถูกจัดเรียงในลำดับย้อนกลับและแสดงบนคอนโซล
-
ดัชนีและค่าต่างๆ ได้รับการเตรียมใช้งานเพื่อวัตถุประสงค์ในการแสดงผล
-
รายการมีการวนซ้ำโดยใช้การแจงนับ และดัชนีและค่าจะถูกผนวกเข้ากับรายการที่ว่างเปล่า
-
นี่คือเอาต์พุตที่แสดงบนคอนโซล