Python dictionary and tuple operations (Introduction to Python automated testing 4)

Python dictionary operations

1. Definition

  1. Dictionary used { }to identify
  2. The structure of the dictionary {key:value}key: value pair (Note: the only key is usually a string value can be any type)
  3. Disorder
  4. The key must be unique and an immutable type
    Example: the writing format of a dictionary
dict = {
    
    "name":"blee",
      "sex":"女",
      "age":None
      "job":"测试工程师",
      "hobby":["睡觉","看书"]
      }

Two, get the value

  1. Through the key, the key name must exist in the dictionary, otherwise an error will be reported (this method is used more)
    For example:
dict = {
    
    "name":"blee",
      "sex":"女",
      "age":None,
      "job":"测试工程师",
      "hobby":["睡觉","看书"]
      }
print(dict["name"])

operation result:

blee
  1. 字典名.get(key名), If the key does not exist, no error will be reported.
    For example:
dict = {
    
    "name":"blee",
      "sex":"女",
      "age":None,
      "job":"测试工程师",
      "hobby":["睡觉","看书"]
      }
value = dict.get('job')
print(value)

operation result:

测试工程师

Three, modification and addition

  1. The modification and addition rules in the dictionary are: modify the key if it exists, and add it if the key does not exist
    .
dict = {
    
    "name":"blee",
      "sex":"女",
      "age":None,
      "job":"测试工程师",
      "hobby":["睡觉","看书"]
      }
dict["age"] = 20    # key存在,修改
dict["city"] = "成都"   # key不存在,新增
print(dict)

operation result:

{
    
    'name': 'blee', 'sex': '女', 'age': 20, 'job': '测试工程师', 'hobby': ['睡觉', '看书'], 'city': '成都'}
  1. Set the default value stdefault
    rule: if the key exists, the value will not be changed; if the key does not exist, add the default value.
    Example:
dict = {
    
    "name":"blee",
      "sex":"女",
      "age":None,
      "job":"测试工程师",
      "hobby":["睡觉","看书"]
      }
dict.setdefault("age",25)    # key存在,不改变值
dict.setdefault("city","北京")   # key不存在,新增默认值
print(dict)

operation result:

{
    
    'name': 'blee', 'sex': '女', 'age': None, 'job': '测试工程师', 'hobby': ['睡觉', '看书'], 'city': '北京'}

Four, delete

4.1 of

Format:, del 字典[key]delete a key-value pair
Example:

dict = {
    
    "name":"blee",
      "sex":"女",
      "age":None,
      "job":"测试工程师",
      "hobby":["睡觉","看书"]
      }
del dict["age"]
print(dict)

operation result:

{
    
    'name': 'blee', 'sex': '女', 'job': '测试工程师', 'hobby': ['睡觉', '看书']}

4.2 pop

Format: 字典.pop(key)The value of the deleted item is the return value of the pop() method.
Example:

dict = {
    
    "name":"blee",
      "sex":"女",
      "age":None,
        }
d = dict.pop("name")
print(d)

operation result:

blee

4.3 clear

Format:, 字典.clear()Clear the dictionary
Example:

dict = {
    
    "name":"blee",
      "sex":"女",
      "age":None,
        }
dict.clear()
print(dict)

operation result:

{
    
    }

Five, update

update: refers to update the value of another dictionary to the current dictionary
Example: update the value of dict1 to dict

dict = {
    
    "name":"blee",
      "sex":"女",
      "age":None,
        }
dict1 = {
    
    "city":"成都"}
dict.update(dict1)
print(dict)

operation result:

{
    
    'name': 'blee', 'sex': '女', 'age': None, 'city': '成都'}

Six, get all key/value values

6.1 Get all key values

This is a direct example:

dict = {
    
    "name":"blee",
      "sex":"女",
      "age":None,
        }
keys = list(dict.keys())
print(keys)

operation result:

['name', 'sex', 'age']

6.2 Get all value values

dict = {
    
    "name":"blee",
      "sex":"女",
      "age":None,
        }
values = list(dict.values())
print(values)

operation result:

['blee', '女', None]

Because the content of tuples is relatively small, this article can be finished by the way

Python tuples and sets

1. Definition

  1. Python tuples are similar to lists, except that the elements of tuples cannot be modified;
  2. Use parentheses for tuples and square brackets for lists;
  3. Tuple creation is very simple, just add elements in parentheses and separate them with commas; for example:new1 = ('physics', 'chemistry', 1997, 2000)
  4. Tuples are similar to strings. The subscript index starts from 0 and can be intercepted, combined, etc.;
  5. When the tuple contains only one element, you need to add a comma after the element; for example:new1 = ("one",)
  6. Empty tuple; for examplenew1 = ()

2. Set

set()The function creates a set of unordered non-repeating elements, which can be used for relationship testing, delete duplicate data, and can also calculate intersection, difference, union, etc.
For example: list deduplication

list = ['blee', '女', None,'女']
new_list = set(list)
print(new_list)

operation result:

{
    
    None, 'blee', '女'}

That's it for today's content. The next article will enter if judgments and loop statements. The difficulty has been gradually increasing, but don't give up easily. After all, you can't make it with the salary increase~

Guess you like

Origin blog.csdn.net/dhl345_/article/details/109339370