list index, slice + stride
# li = ['alex', 123, True, (1, 2, 3, 'wusir'), [1, 2, 3, '小明',], {'name':'alex'}] # index, slice, stride # print (li [ 0 ]) # print (li [ 2 ]) # print (li [ 1 : 4 ]) # print (li [: 5 : 2 ]) # print (li [ - 1 : - 3 : - 1 ])
list list (add, delete, modify, check,)
increase
append (append to the end by default)
# li = [ 1 , ' a ' , ' b ' , ' a ' , 2 , 3 , ' old boy ' ] #append # li.append('alex') # print(li.append('alex')) # li.append([1,2,3])
# name_list = ['Brother Jie' ,'Folk Song','Brother Huaxin','Friend Dog','Fangfang']
# while True:
# name = input('Please enter the name of the new employee: Q/q')
# if name.upper() == 'Q':break
# else:
# name_list.append(name)
# print('Added new employee %s' % name)
# print(name_list)
# print(li)
insert(index, element) insert
# li.insert(2,'Little Three') # print(li)
extend (iteratively add, to the end)
# li.extend('ABC') # li.extend([1,2,3]) # print(li)
delete
pop (remove by index, with return value)
# li.pop() # delete the last one by default # li.pop(1) # delete the last one by default # s = li.pop(1) # print(s) # print(li)
remove (remove by element)
# li.remove('a') # print(li)
clear (clears content)
# li.clear() # print(li)
del delete list (delete by index, slice)
of flax [0] of flax [1: 4]
change
(By index, slice to change)
# print(li[1]) # li[1] = 'A' # print(li[1]) # li[1] = [11,22,33,44] # print(li) #Change according to the slice # li[:3] = 'Q' # print(li) # li[:3] = 'alexsb' # print(li) # li[:3] = [11,22,33,44] # print(li )
Check (you can check by index, slice for loop.)
sort (Sort from small to large.)
l1 = [1, 2, 1, 7, 5, 4, 9, 8, 3] l1.sort () print(l1)
sort(reverse=True) (reverse sorting from largest to smallest.)
# l1.sort(reverse=True) # print(l1)
reverse
# l1.reverse() # print(l1)
len length
print (len (l1))
count (check the number of occurrences of an element)
# print(l1.count(1))
index (index by element)
# print(li.index('a'))
List nesting
li = [1,2, ' alex ' ,[ ' 100 ' , ' wusir ' ,99,[]],22 ] # 1, change alex to capitalized Alex # capitalize() # li[2] = 'Alex' # print(li) # print(li[2].capitalize()) # li[2] = li[2].capitalize() # print(li) # 2, turn wusir into all uppercase wusir Put it in place # print(li[3]) # l2 = li[3] # print(l2[1].upper()) # li[3][1] = l2[1].upper() # print (li) #li[3][1] = li[3][1].upper() # print(li) # 3, add 1 to 99 to make 100, put it back # li[3][2] = li[ 3][2] + 1 # print(li)
tuple:
1-tuple is a read-only list and cannot be added, deleted or modified.
2 You can operate on types inside the tuple (except the tuple)
tu1 = (1,2, ' alex ' ,[1, ' taibai ' ],(1,2,3), ' oldboy ' ) tu1[3][1] = 'dddd' print(tu1)
range
As a list of numbers, range (regardless of the head and tail)
# for i in range(100): # [0,1,2,。。。99] # print(i) # for i in range(0,10,2): # [0,1,2,。。。99] # print(i) # for i in range(10,0,-1): # [0,1,2,。。。99] # print(i)