Python Data Types - Lists

li = [ 'runoob', 786 , 2.23, 'john', 70.2 ] 
Format: listname = [element1,element2,element3]
Example: tinyli = [123, ' john ' ]
Note: When there is only one value in the list, add a comma, custom: a =[1,]

1. Check

print (li)   # print the complete list == print li[:] 
print (li[0])   # print the first element of the list Operation: print(li[1-1]) = print(li[0]) 
print (li[1:3])   #Output the elements from the second to the third 
print (li[2:])   #Output all elements from the third to the end of the list 
print ( li [:3])   #Print 1st to 3rd elements 
print (tinyli * 2)   # prints the list twice 
print (li + tinyli)   # prints the combined list 
print (li[:])   # prints all elements 
print (li[1:])   # print the second to the last 
print (li[-1])   #print the last element 
print(li[-2])   #Print the second to last element 
print (li[-2:])   #Print the last two elements 
print (li[1:-1])   #Print the second element to the second to last 
print (li[0:-1:2])   #Print method 1 every 1 element: 
print (li[::2])   #Method 2:

print (li.index( " john " ))   #Print the position format of the element value: list name.index (element value), if there are multiple johns, only the first john can be taken 
print ([li.index( " john " )])   # Take the element position, li[li.index("john")]: print the element value according to the element position 
print (li[li.index( " john " )])

print (li.count( " john " ))   #Print the number of repetitions of an element value in the list Format: list name.count(element value) 

li.reverse() #Reverse   the position of the elements in the list Format: list name .reverse(); print again 
print (li)

li.sort() #The   elements in the list are sorted by symbols, numbers, uppercase, lowercase format: list name.sort(); print again; reverse order: sort(reverse=True) 
print (li)   #Note that only It is the same data type to sort, there are different data types in the list, this method will report an error 
b = sorted(li) #Method   2 print 
( b)    #Same note

print ( ' 786 '  in li)   #Find whether the specified element value exists in the list, pay attention to the data type format: element value in list name # Return boolean type true or false 
li10 = li * 10   # Repeat list format: new list name = list name 1 * times 
print (li10)

2. Change

li[0] = ' tom ' #Modify   the element value format of the specified position: list name [element position] = new element value 
li[1:3] = [ ' hjc1 ' , ' hjc2 ' ] #   Modify multiple element values ​​at the same time Format: list name[element position:element position]=['element value','element value']

3. Delete

Method 1: To know the location
del li[3] #Delete the element format at the specified position: del list name [element position] del li #Directly delete this variable of li

Method 2: To know what the value of the element is, you can only delete it once and use the loop multiple times
li.remove( " mayun " ) #Remove the specified element value format in the list: list name.remove(element value)

Method 3: No index is added in the parentheses, the last element is deleted by default, add, specify the deletion, and return the deleted value 
lsit.pop() #Delete the last element by default Value format: list name.pop( )

li.clear() #Clear all the list

4. Increase

li.insert(3, ' 111 ' ) #Insert a new element format at the specified position: list name.insert(position, element value) 
li.append( ' mayun ' ) #Add an element format at the end: list name.append( Element value) 
li3 = li + li1 #Merge list format: new list name = list name 1 + list name 2 li.extend 
(li1) #Merge one list into another list format: list name 1.extend(list name 2 ), which is equivalent to adding multiple appends multiple times 
li = [ ' alex ' , ' hjc ' ]
 print ( ' 333 ' .join(li)) #Splice result: alex333hjc

5. Other operations

# count: count the number of times an element appears in the list 
t = [ ' to ' , ' be ' , ' or ' , ' not ' , ' to ' , ' be ' ]
 print (t.count( ' to ' ) )
 # 2

# extend: modifies the extended list, while the original join operation (+) does not, it returns a brand new list 
a = [ ' a ' , ' b ' , ' c ' ]
b = ['1', '2', '3']
a.extend(b)
print (a)
 # ['a', 'b', 'c', '1', '2', '3'] 
# a+b: merge two lists into a new list

#loop to get column elements: 
li = [ ' computer ' , ' mouse pad ' , ' U disk ' , ' car ' , ]
 for i in range(len(li)):
     print (i, li[i])
 '' '
0 computers
1 mouse pad
2 U disk
3 cars
'''

#Another way of writing: 
for u in enumerate(li, start=0):   #Add an additional digital key to each element: the default starts from 0 and increases automatically, you can set the default value of start 
    print (u)
 '''
(0, 'computer')
(1, 'mousepad')
(2, 'U disk')
(3, 'car')
'''

Guess you like

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