当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Basic use of pytest
FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
R & D thinking 01 ----- classic of embedded intelligent product development process
Mysql database interview questions
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries(XGBoost)
Amba, ahb, APB, Axi Understanding
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
IOT system framework learning
Small L's test paper
ES6语法总结--下篇(进阶篇 ES6~ES11)
随机推荐
机器学习--线性回归(sklearn)
Matlab learning and actual combat notes
arduino JSON数据信息解析
Gallery之图片浏览、组件学习
OPPO VOOC快充电路和协议
Characteristics, task status and startup of UCOS III
Amba, ahb, APB, Axi Understanding
Linux Yum install MySQL
arduino获取随机数
Kaggle competition two Sigma connect: rental listing inquiries
Pytorch four commonly used optimizer tests
inline详细讲解【C语言】
STM32 how to locate the code segment that causes hard fault
C语言回调函数【C语言】
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
A possible cause and solution of "stuck" main thread of RT thread
Togglebutton realizes the effect of switching lights
The first simple case of GNN: Cora classification
关键字 inline (内联函数)用法解析【C语言】
使用LinkedHashMap实现一个LRU算法的缓存