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

Python interface automation test framework (basic) -- common data types list & set ()

2022-01-31 03:40:36 I am the white moonlight you can't get

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


The next two articles are about variable data types , This paper first list, It is python The most basic data type in , Its elements can be any python data type



keyword :list, Symbol [], Data composition : Can contain any data type :int float str list tuple dict Boolean value ; Separate the different elements with commas

Create a list of


list_1=[]  #  Represents an empty list 
list_2=list(str1) #  Split string , A list of elements []

print(list_2) #  Output :["1","2","3"]
 Copy code 

Value method

  • According to the index value
  • Support slice value [m:n:k]

Additions and deletions

  • There are many ways to increase ,append Additional , stay list The last addition

list1.append("value")  #  Append... Directly after the list ,
print(list1) #  Output :[1,2,3,4,"value"]
 Copy code 
  • insert Insert an element into the specified subscript position , The element in its original position moves back


print(list1) #  Output :[1,2,"element",3,4]
 Copy code 
  • extend(), Parameters are iteratable objects , Such as :set()、str、tuple、list

print(list1) #  Output :[1,2,3,4,"a","b","c"]
 Copy code 
  • Several ways to delete
pop()  #  Delete list The last element in the list 
remove(element)#  Delete the first occurrence of the element 
del list[1] #  Delete list 1 Elements of index location 
clear() #  clear list 
 Copy code 
  • The assignment operation , That is, replacement :list[index_num]=New_value,index_num The value of the position is replaced with New_value


  • Ascending ; Pay attention to the of lifting sequence list Element must be int type
list1.sort() #  Default ascending order ;

print(list1) #  Output :[2,3,5]
 Copy code 
  • Descending ,sort, It can be determined by parameters , The default is False Ascending


print(list1) #  Output :[5,3,2]
 Copy code 
  • trans

print(list1) #  Output :[3,2,1]
 Copy code 

Other built-in functions

  • all() # list As long as there is one element in the element list false Then return to false
  • any() # list As long as there is one in the element list true Then return to true
  • len() # Find the length of list elements
  • count() # Calculation list Number of elements
  • max() # return list The largest value in the element
  • min() # return list The smallest of the elements


set(), Curly braces {} Express , Why talk to list Type one piece , Because it can be in list Switch between , And right list Elements have the property of de duplication

establish set() aggregate

s=set()  #  Represents an empty set 

print(s) #  Output :set().  Why not {}, because {} It's not worth it dict type 


print(s) #  Output :{1,2,3}
 Copy code 

Additive elements , If there are duplicate elements that are overwritten



print(se) #  Output :{1,2,3,4}
 Copy code 
  • It is an immutable data type

copyright notice
author[I am the white moonlight you can't get],Please bring the original link to reprint, thank you.

Random recommended