1. dictionary definition
And a list of differences
- The list is an ordered collection of objects
- A dictionary is unordered collection of objects
Dictionaries with {} is defined
- Key key is the index
- Value value data
- Keys and values used between : dividing
- Key must be unique in
- Value may take any rational data, but can only use the key strings , numbers , or tuple
definition
Dictionary name = {key1: value1, key2: value2, key3: value4}
1 people = {"name": "小明", 2 "age": 18, 3 "height": 1.75} 4 print(people) # {'name': '小明', 'age': 18, 'height': 1.75}
2. Operating dictionary
. 1 people = { " name " : " Bob " } 2 Print (people) # { 'name': 'Bob'} . 3 Print (people [ " name " ]) # Xiaoming . 4 people [ " name " ] = " John Doe " # modify Dictionary . 5 Print (people) # { 'name': 'John Doe'} . 6 people [ " Age "] = 18 # not the new . 7 Print(people) # { 'name': 'John Doe', 'Age': 18 is} . 8 Print (. people POP ( " Age " )) # delete an existing key pair deletion return value . 9 Print (people ) # { 'name': 'John Doe'} 10 . 11 Person = { ' name ' : ' John Doe ' , ' Age ' : 18 is } 12 is Print ( len (Person)) # 2 13 is temp_dict = { " height " :1.75} 14 . Person Update (temp_dict) # If duplicate keys, overwrites the previous value pairs 15 Print (Person) # 'name': 'John Doe', 'Age': 18 is, 'height': 1.75} 16 . 17 for K in Person: 18 is Print ( " % S -% S " % ( K , Person [ K ])) . 19 20 is "" " name - John Doe 21 is Age - 18 is 22 is height - 1.75 " ""
3. The combination of dictionary and a list of
More dictionaries can be set in a list
1 card_list = [ 2 {"name": "张三", 3 "QQ": 123456, 4 "phone": 10010}, 5 {"name": "李四", 6 "QQ": 654321, 7 "phone": 10086} 8 ]