List addition, deletion, modification, nesting, etc., and tuple query

List of items

1. What is a list

List is a variable data type

The list is represented by [] for each element, separated by commas. The list can contain anything and the object of the object

List can hold a large amount of data

lst = ["Zhao Si","ABC",123,["Ha-ha","Roar"],("I","call","element","group")]

2. Index and slice of list

Lists, like strings, have indexes and slices, but the cut out content is lists

lst = ["Zhao Si","Nicholas","Jack Ma","Wang Jianlin"]



Index subscript starts from zero

lst = ["Zhao Si","Nicholas","Jack Ma","Wang Jianlin"]
print(lst[0:2])    #['Zhao Si', 'Nicholas']
print(lst[:3])     #['Zhao Si', 'Nicholas', 'Jack Ma']


[start position: end position: step size]

II. Addition, deletion and modification of the list

1. increase

Note that list and str are not the same. lst can be changed, so they operate directly on the original object

lst = ["Zhao Si","Nicholas","Jack Ma","Wang Jianlin"]
lst.append("Lennon") #['Zhao Si', 'Nicholas', 'Jack Ma', 'Wang Jianlin', 'Lennon']
lst.insert(2,"Wen Dong Xie") #['Zhao Si', 'Nicholas', 'Wen Dong Xie', 'Jack Ma', 'Wang Jianlin', 'Lennon']
lst.extend(["Ultraman",123,"Garden baby"])#['Zhao Si', 'Nicholas', 'Wen Dong Xie', 'Jack Ma', 'Wang Jianlin', 'Lennon', 'Ultraman', 123, 'Garden baby']

2. delete

lst = ["Zhao Si","Nicholas","Jack Ma","Wang Jianlin"]
data = lst.pop(3)  #Return deleted data
print(data)         #Wang Jianlin
print(lst)          #['Zhao Si', 'Nicholas', 'Jack Ma']
lst.remove("Jack Ma")   #Delete elements,No error will be reported
print(lst)          #['Zhao Si', 'Nicholas']
del lst[1:3]   #Slice deletion
print(lst)      #['Zhao Si']
lst.clear()  #Clear all
print(lst)      #[]

3. changes

# change

lst = ["Glory of Kings","Cross Fire","Jedi survival","DNF","Monster Hunter world"]
lst[0] = "mine clearance"
print(lst)      #['mine clearance', 'Cross Fire', 'Jedi survival', 'DNF', 'Monster Hunter world']
lst[3] = "Eating chicken"
print(lst)      #['mine clearance', 'Cross Fire', 'Jedi survival', 'Eating chicken', 'Monster Hunter world']
lst[1:3] = ["Crazyracing Kartrider"]  #Delete first,Post addition
print(lst)      #['mine clearance', 'Crazyracing Kartrider', 'Eating chicken', 'Monster Hunter world']
lst[1::2] = ["QQ Fight against landlords","QQ Flying car"]  #When the slice is modified,If the step size is not 1,Note the number of elements
print(lst)      #['mine clearance', 'QQ Fight against landlords', 'Eating chicken', 'QQ Flying car']

4. check

List is an iterative object

lst = ["Glory of Kings","Cross Fire","Jedi survival","DNF","Monster Hunter world"]
for el in lst:

5. Other operations

sort() sort default ascending

sort(reverse=True) descending

reverse() flip

len() for length

lst = ["upper","sea","yes","One","Temporary","sea","City"]
# c = lst.count("sea")  #query"sea"Number of occurrences
# print(c)

lst = [77,3,52,6,11,7,88]
lst.reverse()  #Reverse order  [88, 7, 11, 6, 52, 3, 77]
lst.sort()   #Ascending order,Default is ascending
print(lst)  #[3, 6, 7, 11, 52, 77, 88]
lst.sort(reverse=True)   #Descending order
print(lst)      #[88, 77, 52, 11, 7, 6, 3]



Nesting of lists

Using dimension reduction operation, one layer at a time

lst = [1, "Tai Chi", "wusir", ["Painful pain", ["Coca Cola"], "Wang Jian Lin"]]
#find wusir
#Find Taibai and wusir
#Find white
# take wusir Get. And then write. Throw it back.
s = lst[2]
s = s.capitalize()
lst[2] = s
# Abbreviation
lst[2] = lst[2].capitalize()
# Replace Taiji with Taiji
lst[1] = lst[1].replace("A kind of", "A kind of")
# Replace A kind of pain with A kind of chemical pain
lst[3][0] = lst[3][0].replace("A kind of", "turn")



Four Dian tuple


Read only list. You can only read and do nothing

Use () for tuples

If there is only one element in the tuple (element,)

Empty tuple()

Tuple is an iteratable object, which can use for loop

tu = (1,"Li Bai","Du Fu","Bai Juyi","Wang Wei")

print(tu[3])  #Tuple slice or tuple

# for Cycle through the epoch group
for el in tu:


Five, range

range(n) from 0 to n-1

range(m,n) from m to n-1

range(m,n,q) from m to n-1


Use the range and for loops to get the index in the list

for i in range (len):

i. index

List [i] elements

Keywords: Python

Added by Ben Cleary on Tue, 21 Jan 2020 18:37:17 +0200