From this codes,we will know the definition of dictionary and there are some explanation under this code to understand the meaning.

1 alien={'color':'blue','point':6}#the definition of dictionary
2 #in python ,dictionary is represented by a series of key-value pairs place in {}
3 print(alien['color'])#print the value of color
4 #from this example,we can find the way to receive the appointed value from dictionary
5 value_color=alien['color']6 value_point=alien['point']7 print(f"your face is {value_color}")#using variables in strings
8 print(f"your face is {value_color},and have {value_point} buttons in your clothes")9 #modify the value in key-value pairs is the same of C,you just overwrite the old value with new values
10 del alien['color']11 print(alien)12 alien['age']=16
13 alien['leg']=5
14 value_mother=alien.get('mother','no mother values assigned')15 #if the value of mother is not assgned,it will print'no mother values assigned',rather show error
16 print(value_mother)17 value_age=alien.get('age','no age values assigned')18 print(value_age)19 #from this example ,we can find if we can not sure there is the appointed value in dictionary,we should consider using .get() firstly

Code running result:

If we only read this code,we will feel bored quickly,so i write some examples:

1 #We have already know the traversal in Python,the first example is tell this.
2 favorite_fruit={3     'Tom':'banana',4     'Jack':'apple',5     'Kim':'strawberry',6     'Limin':'grapefruit'
7 }8 for name,fruit infavorite_fruit.items():9     print(f"\nName:{name}")10     print(f"Fruit:{fruit}")11 #now,we have one question for whether we could get the key only or just get value
12 #in order to meet the above needs,we should use .key() method
13 print("\n")14 Good_Students=['Jack','Kim']15 '''
16 (solution 1)if 'Fu' not in favorite_fruit.keys():17 print("Fu,please tell me waht fruit you like")18         '''
19 for name infavorite_fruit.keys():20     print(name.title())21     if name inGood_Students:22         fruit=favorite_fruit[name].title()#get the specified value
23         print(f"\t {name.title()},i know you like {fruit}")#the same meaning
24     if 'Fu' not infavorite_fruit.keys():25         print("Fu,please tell me waht fruit you like")26         '''
27 #if the code is set in this place ,we will find it will be print many times!28 #so,we need one solution to solve it29 #for instance,we can adjust the place the code is30 (solution 2)if 'Fu' not in favorite_fruit.keys():31 #print("Fu,please tell me waht fruit you like")32         '''
33 #we have solved the above problem,so why not try to traversal the value?
34 print("there are many kinds of fruit that student like\n")35 for fruits infavorite_fruit.values():36     print(fruits.title())37     #find the fruit which initials is A
38     if(fruits[:1].title()=='A'):39         print("\t the fruit's initials is A")40 #however,if there are many duplicate values in the set,that means the program will do useless work
41 #To solve the problem,we can use .set()
42 for fruits inset(favorite_fruit.values()):43     print(fruits.title())44     #find the fruit which initials is A
45     if(fruits[:1].title()=='A'):46         print("\t the fruit's initials is A")47 #the result is not change
48 #now,we have another question,we have find the way to get values through keys,so,could we find the keys through values?
49 #the answer is Yes,we still ues the same code,and add some new code above the original code
50 defget_key(key,value):51     return [r_key for (r_key,r_value) in key.items() if r_value==value]52 for fruits infavorite_fruit.values():53     print(fruits.title())54     #find the fruit which initials is A
55     if(fruits[:1].title()=='A'):56         find_value=fruits57         goal_key=get_key(favorite_fruit,find_value)58         final_key=goal_key[0]#the value is stored in list
59         print(f"\t {final_key} is the goal keys")

However,at this time,someone maybe think if therer are many data that are related to be stored in the dictionary,it may seem stupid if we still choose to enter the data one by one,

we can use nested,and this is the content of my next blog.

Thank you for your reading!

标签: none

添加新评论