List element correspondence and list operations

>>> x = ['a','b','c','d','e']
>>> y = [1,2,3,4,5]
>>> for i,j in zip(x,y):
		print(i,j)	
a 1
b 2
c 3
d 4
e 5

for i in sorted(sor):排序
for i in sorted(set(sor)): sort to remove duplicates
for i in reversed(range(len(lis))):倒序

  

The query format is:
list[start position:end position:step]
The characteristics of the list are that the head does not care about the buttocks, including the beginning but not the end
The default step size is positive sequence, the step size is 1; it can be modified
Such as: 1, 2, 3...
The step size can also be negative, and the query results are in reverse order
Such as: -1, -2, -3.....
When the sequence is positive, the start and end are also positive sequence, the start is in the front and the end is in the back
When the order is reversed, the start and end must also be in reverse order, with the start at the back and the end at the front.

Reverse order + step size
lis1 = ['a', 'b', 'c', 'd', 'e', ​​'f']
>>> lis1[::-1]
['f', 'e', 'd', 'c', 'b', 'a']
>>> lis1[-1:0:-1]
['f', 'e', 'd', 'c', 'b']
>>> lis1[::2]
['a', 'c', 'e']
add element
lis1.append("g")
>>> lis1
['a', 'b', 'c', 'd', 'e', 'f', 'g']
remove element
>>> li = lis1.pop()
>>> li
'g'
>>> lis1
['a', 'b', 'c', 'd', 'e', 'f']
insert element
>>> lis1.insert(0,"y")
>>> lis1
['y', 'a', 'b', 'c', 'd', 'e', 'f']
Number of occurrences + location of occurrence
>>> lis2 = ['a','b','a','a','b','c']
>>> lis2.count('a')
3
>>> lis2.count('b')
2
>>> lis2.index("b")
1
>>> lis2.index('b',2)
4
>>> lis2.index('c')
5
Sort + Reverse
>>> lis3 = [3,5,1,8,2,4,6,3]
>>> lis3.sort()
>>> lis3
[1, 2, 3, 3, 4, 5, 6, 8]
>>> lis3.sort(reverse=-1)
>>> lis3
[8, 6, 5, 4, 3, 3, 2, 1]
Extended list
>>> lis3[len(lis3):]=["a","b","c"]
>>> lis3
[8, 6, 5, 4, 3, 3, 2, 1, 'a', 'b', 'c']
>>> lis3.extend(['d','e','f'])
>>> lis3
[8, 6, 5, 4, 3, 3, 2, 1, 'a', 'b', 'c', 'd', 'e', 'f']
deduplication
>>> l1 = ['b','c','d','b','c','a','a']
>>> l2 = list(set(l1))
>>> l2
['c', 'b', 'a', 'd']
>>> l2=list(set(l1))
>>> l2.sort(key=l1.index)# The order remains unchanged after deduplication
>>> l2
['b', 'c', 'd', 'a']
>>> l1
['b', 'c', 'd', 'b', 'c', 'a', 'a']
>>> l2=list({}.fromkeys(l1).keys())
>>> l2
['b', 'c', 'd', 'a']
>>> l1
['b', 'c', 'd', 'b', 'c', 'a', 'a']
>>> l2=[]
>>> for i in l1:
	if not i in l2:
		l2.append(i)
>>> l2
['b', 'c', 'd', 'a']
>>> l1
['b', 'c', 'd', 'b', 'c', 'a', 'a']
>>> l2 = []
>>> [l2.append(i) for i in l1 if not i in l2]
[None, None, None, None]
>>> l2
['b', 'c', 'd', 'a']


import copy
person = ['name',['a',100]]
p1 = copy.copy(person)
p2 = person[:]
p3 = list(person)
Shallow copy to create a joint account
p1 = person[:]
p2 = person[:]
p1[0] = 'A'
P2[0] = 'B'
p[1][1] = 50
print(p1,p2)

Tuple Turple has only count and index

  

Guess you like

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