The method of calling the key in the dictionary in python:
Use the list() method to convert the keys in the dictionary into a list, and then use the list index value to call the specified key.
# - *- coding: utf-8-*-
# Define a dictionary
dic ={'Plot':11,'crime':10,'action':8,'love':3,'comedy':2,'adventure':2,'Suspense':2,'Horror':2,'Fantasy':1}
# Convert the keys and values in the dictionary into a list through the list
keys =list(dic.keys())
values =list(dic.values())
# Result output
print("The keys list is:",end='')print(keys)print("The values list is:",end='')print(values)
# Call key in dictionary
for index inrange(len(keys)):print(keys[index])
The output is as follows:
The list of keys is: ['story','crime','action','love','comedy','adventure','suspense','thriller','fantasy']
The list of values is: [11, 10, 8, 3, 2, 2, 2, 2, 1]
Plot
crime
action
love
comedy
adventure
Suspense
Horror
Fantasy
Content expansion
Python has built-in dictionary type, uses key-value (key-value) storage, and has extremely fast search speed;
dict advantages:
Fast binary search efficiency
Store large amounts of relational data
Features: The dictionary is disordered
So far, this article on how python calls the dictionary key is introduced. For more relevant examples of python calling the dictionary key method, please search for the previous article of ZaLou.Cn or continue to browse the related articles below. Hope you will support more in the future ZaLou.Cn!
Recommended Posts