current position:Home>Python interface automation test framework (basic) -- common data types Dict

Python interface automation test framework (basic) -- common data types Dict

2022-01-31 05:15:08 I am the white moonlight you can't get

「 This is my participation 11 The fourth of the yuegengwen challenge 6 God , Check out the activity details :2021 One last more challenge

Preface

The next two articles are about variable data types , Finally dict, From the author's experience , The order of common data types should be dict、list、str、tuple、bool( Often used as a judgment condition )、int.< Of course, others have different views >

image.png

Dictionaries

keyword :dict, Symbol {}, It is a variable container model and can store any type of object ; With key:value Save data in the form of ,key: only , Non iteratable data types :int float Boolean value Tuples &value: Can contain any data type :int float str list tuple dict Boolean value

Create a dictionary

  • Code demonstration : The immutable data type is key
dict_1={}  #  Represents an empty dictionary 

dict_2={"a":"b"}

dict_3={(1,):2,1.1:2,True:False} 
 Copy code 

Value method

dict_1={"k1":"v1"}

dict_1[key] #  That is, get the specified key Value 

dict_1.get(key) # obtain key Value , There is no return none, It is more flexible than the previous dictionary 
 Copy code 

Additions and deletions

  • Add to change : The assignment operation , If key non-existent , It's new , The opposite is to modify
dict_1={}
dict_1["key"]="value" 

print(dict_1)={"key":"value"}
 Copy code 
  • dict Delete except del Others are implemented through functions
dict_1={"k1":"v1","ke":"ve"}

v=dict_1.pop("k1")  #  Delete function , And return to delete key Of value

print(v) #  Output :v1

dict_2=dict_1.popitem()  #  Randomly delete a group key:value And back to tuple

print(dict_2) #  Output :("ke","ve")

clear() #  Empty dictionary   no return value , primary dict_1 Will become {}

del dict_1[key]  Delete the specified key
 Copy code 
  • update The update function can only be dict type
dict1={"k1":"v1"}

dict1.update({1:2})

print(dict1) #  Output :{'k1': 'v1', 1: 2}
 Copy code 

Built in functions

  • items(), among dict The key value pair of becomes an element of a tuple , return list form , But it's not list data type , It is dict_items
dict1={"ke":"ve","k1":"v1"}

li=dict1.items()

print(li) #  Output :dict_items([{"ke":"ve"},{"k1":"v1"}])
 Copy code 
  • values(), obtain dict All of the objects value, With list form , But not of any kind , It is dict_values
dict1={"ke":"ve","k1":"v1"}

li=dict1.values() 

print(li) # dict_values(['ve', 'v1'])
 Copy code 
  • keys(), obtain dict All of the objects key, With list form , But not of any kind , It is dict_keys
dict_1={"ke":"ve","k1":"v1"}

li=dict_1.keys()

print(li) #  Output :dict_keys(['ke', 'k1'])
 Copy code 
  • len() Calculate the length of the variable , stay dict Statistics in key Total of
  • has_key(key) Used to determine key Whether there is dict In the object , Otherwise return to False
dict1={"k1":"1","a":"b","k1":"2"}

b=dict1.has_key("kk")

# Python 3.X  The method is not supported .
print b #  Output :False
 Copy code 

Extended features

We know dict Type of key Must be the only , Immutable data types , If key Duplicate occurs , It only remembers the last value


dict1={"k1":"1","a":"b","k1":"2"}

print(dict1) #  Output :{"k1":"2","a":"b"}
 Copy code 

copyright notice
author[I am the white moonlight you can't get],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310515059171.html

Random recommended