当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Amba, ahb, APB, Axi Understanding
Principle and implementation of MySQL master-slave replication
Machine learning -- linear regression (sklearn)
Unit test - unittest framework
[template] KMP string matching
ES6语法总结--下篇(进阶篇 ES6~ES11)
AMBA、AHB、APB、AXI的理解
Mall project -- day09 -- order module
FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
Gallery's image browsing and component learning
随机推荐
Pytorch-温度预测
E-commerce data analysis -- salary prediction (linear regression)
互聯網協議詳解
Analysis of charging architecture of glory magic 3pro
Detailed explanation of Union [C language]
Those commonly used tool classes and methods in hutool
vim命令行笔记
OSPF message details - LSA overview
Correspondence between STM32 model and contex M
Funny cartoon: Programmer's logic
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
Gallery's image browsing and component learning
Gallery之图片浏览、组件学习
Time slice polling scheduling of RT thread threads
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
E-commerce data analysis -- User Behavior Analysis
map文件粗略分析
Small L's test paper
【ESP32学习-2】esp32地址映射
小天才电话手表 Z3工作原理