当前位置:网站首页>Use of lists

Use of lists

2022-07-06 12:04:00 A geek is as deep as the sea

How to create a list
list=[ Elements 1, Elements 2, Elements 3,···]
And the element can be Any kind of , such as :


>>> name=[" Merge "," Sure "," Put the other side "," list "," Value "," Merge in ",11,'f']
>>> name
[' Merge ', ' Sure ', ' Put the other side ', ' list ', ' Value ', ' Merge in ', 11, 'f']

View the properties of the list

>>> type(name)
<class 'list'>
>>> 

List related operations
name.append(); # Additional , The data will be appended to the tail
name.clear(); # clear list
name.copy() # Copy
name.extend() # Merge , You can merge the values of another list
name.index() # Returns the subscript of the first element
name.pop() # Deletes the specified element , Delete the last... By default
name.remove() # Function to remove the first match of a value in the list .
name.sort() # Used to sort the original list , If you specify parameters , Then use the comparison function specified by the comparison function .
name.count() # Returns the number of occurrences of the element
name.insert() # Insert , It can be inserted anywhere
name.reverse() # Reverse list of elements .

Splicing of lists ” “.join(list) Method

Specific explanation

Add to list

name.append(); # Additional , The data will be appended to the tail

>>> name
[' Merge ', ' Sure ', ' Put the other side ', ' list ', ' Value ', ' Merge in ', 11, 'f']
>>> name.append(" tianjin ")
>>> name
[' Merge ', ' Sure ', ' Put the other side ', ' list ', ' Value ', ' Merge in ', 11, 'f', ' tianjin ']

name.insert() # Insert , It can be inserted anywhere

>>> name
[' Merge ', ' Sure ', ' Put the other side ', ' list ', ' Value ', ' Merge in ', 11, 'f', ' tianjin ']
>>> name.insert(1," Can not be ")
>>> name   
[' Merge ', ' Can not be ', ' Sure ', ' Put the other side ', ' list ', ' Value ', ' Merge in ', 11, 'f', ' tianjin ']

name.extend() # Merge , You can merge the values of another list

>>> name=[" Merge "," Sure "," Put the other side "," list "," Value "," Merge in ",11,'f']
>>> okk=[" Merge "," Sure "]
>>> name.extend(okk)
>>> name
[' Merge ', ' Sure ', ' Put the other side ', ' list ', ' Value ', ' Merge in ', 11, 'f', ' Merge ', ' Sure ']

name.index() # Returns the subscript of the first element

>>> name
[' Merge ', ' Sure ', ' Put the other side ', ' list ', ' Value ', ' Merge in ', 11, 'f', ' Merge ', ' Sure ']
>>> name.index(" Merge ")
0

name.count() # Returns the number of occurrences of the element

>>> name
[' Merge ', ' Sure ', ' Put the other side ', ' list ', ' Value ', ' Merge in ', 11, 'f', ' Merge ', ' Sure ']
>>> name.count(" Merge ")
2

name.sort() # Used to sort the original list , If you specify parameters , Then use the comparison function specified by the comparison function .

list.sort( key=None, reverse=False)

key – It's basically a comparison element , There is only one parameter , The parameters of the specific function are taken from the iterable object , You can iterate over an element in the object to sort .
reverse – Sort rule ,reverse = True Descending , reverse = False Ascending ( Default ).

Usage mode

>>> name = ['e', 'a', 'u', 'o', 'i']
>>> name.sort() # The default parameter is in ascending order 
>>> name
['a', 'e', 'i', 'o', 'u']
>>> name.sort(reverse = True)
>>> name  # Descending order 
['u', 'o', 'i', 'e', 'a']

name.reverse() # Reverse list of elements .

>>> name = ['e', 'a', 'u', 'o', 'i']
>>> name.reverse()
>>> name
['i', 'o', 'u', 'a', 'e']

name.copy() # Copy


>>> name = ['e', 'a', 'u', 'o', 'i']
>>> come=name.copy()
>>> come
['e', 'a', 'u', 'o', 'i']

About the deletion of the list , Except for the three given in the function , You can also use del Keyword to delete .
Here we will first explain how to use the three functions , Explain how to use keywords

name.pop() # Deletes the specified element , Delete the last... By default , And return the deleted content

>>> name = ['e', 'a', 'u', 'o', 'i']
>>> name.pop(1)   
'a'
>>> name
['e', 'u', 'o', 'i']
>>> name.pop()
'i'
>>> name
['e', 'u', 'o']

Receive and return deleted content

>>> name = ['e', 'a', 'u', 'o', 'i']
>>> num = name.pop()
>>> name
['e', 'a', 'u', 'o']
>>> num
'i'

name.remove() # Function to remove the first match of a value in the list .

>>> name = ['e', 'i','a', 'u', 'o', 'i']
>>> name.remove('i')
>>> name
['e', 'a', 'u', 'o', 'i']

name.clear(); # clear list

>>> name = ['e', 'i','a', 'u', 'o', 'i']
>>> name.clear()
>>> name
[]

Use del Keyword deletes the specified element in the list


>>> name = ['e', 'i','a', 'u', 'o', 'i']
>>> del name[1]
>>> name
['e', 'a', 'u', 'o', 'i']

About the splicing of lists Use join() Method

>>> lim=["alex","eric","rain"]
>>> '_'.join(lim)
'alex_eric_rain'

In the above procedure Underline 【 _ 】 You can put anything you want ,

above , About the list first , Later, if you are proficient, you can add .

原网站

版权声明
本文为[A geek is as deep as the sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060913282121.html