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


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 >



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 


 Copy code 

Value method


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

 Copy code 
  • dict Delete except del Others are implemented through functions

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


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


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


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


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


# 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


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.

Random recommended