List of Python learning 1

Python learning 1

Built in method

1 increase

append() append after the list

list=[12,"123"."xiaoming"]
list1.append("heihei")#Append after list
print(list)    #output[12,"123"."xiaoming","heihei"]

insert(index, "content") add anywhere

list=["Zhang San","lisui","wangwu"]
list.insert(2,"zhaoliu")
print(list)#Output["Zhang San","lisui","zhaoliu",wangwu"]

extend(list), merge two lists -- meaning of extension

list1=["Zhang San","lisui","wangwu"]
list2=[12,"123"."xiaoming"]
list1.extend(list2)
print(list1)#output["Zhang San","lisui","wangwu",12,"123"."xiaoming"]

2 modification

a[index] = "new value"

list1=["Zhang San","lisui","wangwu"]
list1[1] = "Li Si"
print(list1)#Output["Zhang San","Li Si","wangwu"]

a[start:end] = [a,b,c]

list1=["Zhang San","lisui","wangwu"]
list1[1:] = ["Li Si","Wang Wu"]
print(list1)#Output["Zhang San","Li Si","Wang Wu"]

3 delete

remove("content")

list1=["Zhang San","lisui","wangwu"]
list1.remove("lisui")
print(list1)#Output["Zhang San","wangwu"]

pop(index) is deleted according to the subscript

list1=["Zhang San","lisui","wangwu"]
list1.pop(1)
print(list1)#Output["Zhang San","wangwu"]

del a, del a[index] del is a delete object

list1=["Zhang San","lisui","wangwu"]
del list1[1]
print(list1)#Output["Zhang San","wangwu"] 
del list1#delete object
print(list1)#Error reported because the object was deleted

a.clear() clears the list

list1=["Zhang San","lisui","wangwu"]
list1.clear()
print(list1)#Output []

4 sorting

Sort() sort from small to large

list1=[15,345,7,4,23,9,0]
list2=[15,345,7,4,23,9,0]
list1.sort()#from small to large
list1.sort(reverse=False)#From big to small
print(list1)#Output sorted value[0,4,7,9,15,23,345]

reverse() reverse

list1=[15,345,7,4,23,9,0]
list1.reverse()
print(list1)#Output reversed value[0,9,23,4,7,345,15]

5. Identity judgment type [object] determines the type of object

type(a) == "list"

6 other methods

count() determines the number of times an element appears in the list

list1=["wto","weilcome","hello","first","hello"]
count = list1.count("hello")
print(count)#output 2

index() determines where an element first appears in the list

list1 = ["wto", "weilcome", "hello", "first", "hello"]
index = list1.index("hello")
print(index)# Output 2

Tags: Python

Posted on Sun, 03 May 2020 08:26:05 -0700 by wee493