The basic magic of lists

1.li.append (5) added!

li = [12,54,45,45 ]
li.append ( 5 )
 print (li)

[12,54,45,45,5]

2.li.clear() Clear!

li = [12,54,45,45 ]
li.clear()
print (li) 

[]

3.v = li.copy() Shallow copy!

li = [12,54,45,45 ]
v = li.copy ()
 print (v)

4.v = li.count(22) counts the number of occurrences of the element

li = [12,54,45,45 ]
v = li.count (22 )
 print (v)

5.li.extend([9898,"incredible"]) executes the for loop, adding the things in the parentheses separately instead of appending them as a whole to the list

li = [12,54,45,45 ]
li.extend([ 9898, " Fantastic " ])
 print (li) 

[12, 54, 45, 45, 9898, 'Fantastic']

5.v = li.index(12) Get the index position, start looking from the left, you can set the search range

li = [12,54,45,45 ]
v = li.index (12 )
 print (v) 

0

6.li.insert(0,99) inserts an element at the specified position

li = [12,54,45,45 ]
li.insert(0,99)
print(li)

[99, 12, 54, 45, 45]

7.li.pop() v =li.pop() deletes the last value by default, you can delete a certain value (delete by index, for example, the parameter is 1 to delete the second element), and you can use assignment to get the deleted value

li = [12,54,45,45 ]
v = li.pop ()
 print (li) 

[12, 54, 45]

8.li.remove(54) delete the value specified in the list, and start from the left

li = [12,54,45,45 ]
li.remove ( 54 )
 print (li) 

[12, 45, 45]

9.li.reverse() reverses the current list

li = [12,54,45,45 ]
li.reverse()
print (s) 

[45, 45, 54, 12]

10.li.sort() li.sort(reverse=Ture) sort, the first one is from small to big, the second is from big to small

li = [12,45,78,54 ]
li.sort ()
print (li) 

[12, 45, 54, 78]

 

Guess you like

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