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"]

print(lst[1])
print(lst[2])
print(lst[3])

 

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

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

2. delete

#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:
    print(el)

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]
print(lst)
lst.reverse()  #Reverse order  [88, 7, 11, 6, 52, 3, 77]
print(lst)
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
print(lst[2])
#Find Taibai and wusir
print(lst[1:3])
#Find white
print(lst[1][1])
# take wusir Get. And then write. Throw it back.
s = lst[2]
s = s.capitalize()
lst[2] = s
print(lst)
# Abbreviation
lst[2] = lst[2].capitalize()
print(lst)
# Replace Taiji with Taiji
lst[1] = lst[1].replace("A kind of", "A kind of")
print(lst)
# Replace A kind of pain with A kind of chemical pain
lst[3][0] = lst[3][0].replace("A kind of", "turn")
print(lst[3][0])
lst[3][1].append("Sprite")
print(lst)

 

 

Four Dian tuple

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)

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

# for Cycle through the epoch group
for el in tu:
    print(el)

 

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