Add, delete, modify, search and sort python lists

python3.0

1. List

a = ['1','2','3','4','5',]

2. Find (there are three commands) (indexes start from 0)

1. .count #refers to the number of times an element appears in the list

a.count('2')

print(a)  # ==> 2

2. .index #Find its corresponding position according to the subscript

a.index(2)

print(a)   # ==>'3'

3. Determine whether an element is in the list a

'6' in a    # ==> Flase

3. Increase (there are three commands)

1. a.append() #Append command

a.append(6) 

print(a)   # ==>a = [1,2,3,4,5,6]

2. a.insert() #You can insert elements at any position in the list

a.insert(2,5)

print(a)   # ==>a = [1,2,5,3,4,5]

3、 a.hears

Fourth, modify (there are two commands)

1. a[index] = "new value"

a[1] = 9

print(a)   # ==>a = [1,9,5,3,4,5]

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

a[1:4] = ['a', 'b', 'c']

print(a)    # ==>a = [1,'a','b','c',4,5]

a[1:4] = ['a', 'b']

print(a) # ==>a = [1,'a','b',4,5] #If the input list is inconsistent with the number of sliced ​​lists, only the inserted list will be displayed, and the remaining list of the slice will be empty

5. Delete

1、    remove()

a.remove(5) #Enter the value, find the first element and delete it

print(a)   # ==>a = [1,2,3,4]

2. pop(index) #Enter the subscript

a.pop(0) 

print(a)    # ==>a = [2,3,4,5]

3 a of a ; of a [index]

del a # delete the entire list

del a[index] #delete an element in the array

4. clear() to clear

a.clear() 

print(a)   # ==>a = []

6. Sort

1. sort() #Sort in ascending order

a.sort()

print(a)    #==>a = [1,2,3,4,5]

2. reverse() #Reverse the head and end of the list

a.reverse()

print(a)    #==>a = [5,4,3,2,1]

  ==============================

To sort in descending order

a.sort(reverse = True)

print(a)

  ==============================

7. Identity judgment

type(a) is list   #==>   True

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324996749&siteId=291194637