python (dictionary function / operation)

I. dictionary operation

1. Add key value pair to dictionary

  • dict["key"] = "value"
    stu = {"name":"zhangsan","age":"18"}
    stu["sex"] = "boy"
    print stu
  • setdefault()
    stu = {"name":"zhangsan","age":"18"}
    stu.setdefault("sex")
    print stu    #Result {'age': '18', 'name': 'zhangsan', 'sex': None}
    
    stu = {"name":"zhangsan","age":"18"}
    stu.setdefault("sex","boy")
    print stu  #Result {'age': '18', 'name': 'zhangsan', 'sex': 'boy'}

2. Replace the value of the specified key in the dictionary

  • dict["key"] = "value"
    stu = {"name":"zhangsan","age":18,"sex":"boy"}
    #Replacement value boy by gril
    stu["sex"] = "girl"
    print stu

3.update() append all items in one dictionary to another

a = {"name":"zhangsan","sex":"boy"}
b = {"age":18}

a.update(b)    #Go to the dictionary a Add dictionary to b Content
print a
print b

4.get() dictionary value -- > when the extracted key does not exist, no error will be reported and it is empty.

a = {"name":"zhangsan","sex":"boy"}

print a.get("name")
#When the retrieved key does not exist, no error is reported, and the return value is None
print a.get("age")

5.keys () takes all keys from the dictionary

  • You cannot enter a key in parentheses to get a key value
  • Returns a list of keys
a = {"name":"zhangsan","sex":"boy","age":18}

print a.keys()  #Return to all key lists

6.values () takes all values from the dictionary

  • Returns a list of values
a = {"name":"zhangsan","sex":"boy","age":18}

print a.values()  #Returns a list of all values

7.del() delete key value pair

a = {"name":"zhangsan","sex":"boy","age":18}

del a["name"]
print a    #In result"name":"zhansan"All key value pairs are deleted

8.pop() delete key value pair

  • The parenthesis must be a key and cannot be empty
  • The return value is the value of the delete key
a = {"name":"zhangsan","sex":"boy","age":18}

value = a.pop("name")
print value    #The return value is"zhangsan"
print a    #Dictionaries a Medium"name":"zhangsan"Key value pair deleted

9.popitem() randomly delete key value pairs

  • And returns the tuple of the key value pair
a = {"name":"zhangsan","sex":"boy","age":18}

b = a.popitem()
print b        #The return value is a tuple containing the deleted key value pair
print a        #Dictionaries a A key value pair in is deleted

10.clear() clears key value pairs in the dictionary

  • no return value
  • The original dictionary is empty {}
a = {"name":"zhangsan","sex":"boy","age":18}

a.clear()
print a

11.copy() copy create dictionary

a = {"name":"zhangsan","sex":"boy","age":18}

b = a.copy()
print b        #b yes a Copied dictionary

12.has_key() checks whether the dictionary has the specified key. If yes, the result is True. If no, the result is False.

a = {"name":"zhangsan","sex":"boy","age":18}

print a.has_key("age")      #True
print a.has_key("height")   #False

Keywords: Python

Added by trixx on Wed, 23 Oct 2019 17:18:27 +0300