当前位置:网站首页>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 .
边栏推荐
- IOT system framework learning
- Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
- Unit test - unittest framework
- Selective sorting and bubble sorting [C language]
- Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
- Detailed explanation of nodejs
- GNN的第一个简单案例:Cora分类
- Rough analysis of map file
- Reading notes of difficult career creation
- Small L's test paper
猜你喜欢
荣耀Magic 3Pro 充电架构分析
Linux Yum install MySQL
C language callback function [C language]
Vert. x: A simple TCP client and server demo
FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
Machine learning -- linear regression (sklearn)
电商数据分析--用户行为分析
機器學習--線性回歸(sklearn)
ES6语法总结--下篇(进阶篇 ES6~ES11)
STM32 how to locate the code segment that causes hard fault
随机推荐
Kaggle competition two Sigma connect: rental listing inquiries
XML file explanation: what is XML, XML configuration file, XML data file, XML file parsing tutorial
mysql实现读写分离
Détails du Protocole Internet
Kconfig Kbuild
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
Machine learning -- decision tree (sklearn)
Inline detailed explanation [C language]
gcc 编译选项
【ESP32学习-1】Arduino ESP32开发环境搭建
STM32型号与Contex m对应关系
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
Arm pc=pc+8 is the most understandable explanation
Gallery's image browsing and component learning
Contiki source code + principle + function + programming + transplantation + drive + network (turn)
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
Using LinkedHashMap to realize the caching of an LRU algorithm
Hutool中那些常用的工具类和方法
Mysql database interview questions