当前位置:网站首页>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 .
边栏推荐
- MySQL realizes read-write separation
- C language, log print file name, function name, line number, date and time
- 优先级反转与死锁
- 機器學習--線性回歸(sklearn)
- Internet protocol details
- arduino获取随机数
- ARM PC=PC+8 最便于理解的阐述
- Keyword inline (inline function) usage analysis [C language]
- STM32型号与Contex m对应关系
- Word typesetting (subtotal)
猜你喜欢
Kaggle competition two Sigma connect: rental listing inquiries
机器学习--决策树(sklearn)
STM32 how to locate the code segment that causes hard fault
B tree and b+ tree of MySQL index implementation
ES6语法总结--上篇(基础篇)
PyTorch四种常用优化器测试
ARM PC=PC+8 最便于理解的阐述
小天才电话手表 Z3工作原理
Kconfig Kbuild
Basic knowledge of lithium battery
随机推荐
Kconfig Kbuild
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
The first simple case of GNN: Cora classification
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
I2C总线时序详解
机器学习--线性回归(sklearn)
高通&MTK&麒麟 手機平臺USB3.0方案對比
机器学习--决策树(sklearn)
Linux yum安装MySQL
Implementation scheme of distributed transaction
vim命令行笔记
open-mmlab labelImg mmdetection
Some concepts often asked in database interview
FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
电商数据分析--用户行为分析
互聯網協議詳解
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
PyTorch四种常用优化器测试
MySQL START SLAVE Syntax