当前位置:网站首页>Python basic variable type -- list analysis
Python basic variable type -- list analysis
2020-11-06 20:53:00 【Python advanced】
Python Use list
One 、list
Python One of the built-in data types is list :list.list It's an orderly collection , You can add and remove elements at any time .
such as , List the names of all the students in the class , You can use one list Express :
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates)
Variable classmates It's just one. list.
len() function
1. get list Number of elements :
classmates = ['Michael', 'Bob', 'Tracy']
print(len(classmates))
Visit with an index list Elements in every position in , Remember the index is from 0
At the beginning :
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates[0])
print(classmates[1])
print(classmates[2])
print(classmates[3])
When the index is out of range ,Python Will report a IndexError error , therefore , Make sure that the index doesn't cross the boundary , Remember that the index of the last element is len(classmates) - 1.
If you want to take the last element , Besides calculating the index position , You can also use -1
Do the index , Get the last element directly :
print(classmates[-1])
And so on , You can get the last 2 individual 、 Last but not least 3 individual :
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates[-1])
print(classmates[-2])
print(classmates[-3])
print(classmates[-4])
Of course , Last but not least 4 It's just over the line .
2. list It's a variable ordered list , Go to list Add elements to the end of :
classmates = ['Michael', 'Bob', 'Tracy']
classmates.append('Adam')
print(classmates)
You can also insert elements into the specified location , For example, the index number is 1
The location of :
classmates = ['Michael', 'Bob', 'Tracy']
# Replace
classmates.insert(1, 'Jack')
print(classmates)
pop() function
1. Delete list The element at the end
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates.pop())
print( classmates)
['Michael', 'Jack', 'Bob', 'Tracy']
2. Deletes the element at the specified location , use pop(i)
Method , among i
It's the index position .
classmates.pop(1)
print(classmates)
3. Replace one element with another , It can be directly assigned to the corresponding index position :
classmates = ['Michael', 'Bob', 'Tracy']
classmates[1] = 'Sarah'
print(classmates)
list The data types of the elements inside can also be different , such as :
L = ['Apple', 123, True]
list The element can also be another list, such as :
s = ['python', 'java', ['asp', 'php'], 'scheme']
print(len(s))
it is to be noted that s
Only 4 Elements , among s[2]
Another list, It's easier to understand if you take it apart :
p = ['asp', 'php']
s = ['python', 'java', p, 'scheme']
To get 'php'
Can write p[1]
perhaps s[2][1]
, therefore s
It can be seen as a two-dimensional array , There's a similar three-dimensional 、 4 d …… Array , But it's rarely used .
If one list None of the elements in , It's just an empty list, Its length is 0:
L = []
len(L)
Two 、 summary
This article is based on Python Basics , It mainly introduces Python In the foundation list list , adopt list Two functions of the list , Yes list The grammar is explained in detail , With a wealth of cases , The display of the code effect diagram helps you to better understand .
Use Python programing language , It is convenient for us to understand , I hope it will help you in your study . Want to learn more Python Web crawler and data mining knowledge , Go to a professional website :http://pdcfighting.com/ Want to learn more Python Web crawler and data mining knowledge , Go to a professional website :http://pdcfighting.com/
版权声明
本文为[Python advanced]所创,转载请带上原文链接,感谢
边栏推荐
- Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
- (1) ASP.NET Introduction to core3.1 Ocelot
- Small program introduction to proficient (2): understand the four important files of small program development
- 大数据处理黑科技:揭秘PB级数仓GaussDB(DWS) 并行计算技术
- 大会倒计时|2020 PostgreSQL亚洲大会-中文分论坛议程安排
- Diamond standard
- An article takes you to understand CSS pagination examples
- How to play sortable JS vuedraggable to realize nested drag function of forms
- 【應用程式見解 Application Insights】Application Insights 使用 Application Maps 構建請求鏈路檢視
- Try to build my mall from scratch (2): use JWT to protect our information security and perfect swagger configuration
猜你喜欢
只有1个字节的文件实际占用多少磁盘空间
IPFs rudder filecoin landing at the same time, fil currency price broke a thousand
es创建新的索引库并拷贝旧的索引库 实践亲测有效!
Flink's datasource Trilogy 2: built in connector
Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
Multi robot market share solution
CloudQuery V1.2.0 版本发布
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:曾文旌
事件监听问题
Contract trading system development | construction of smart contract trading platform
随机推荐
C語言I部落格作業03
Jmeter——ForEach Controller&Loop Controller
Application of restful API based on MVC
【学习】接口测试用例编写和测试关注点
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
Pn8162 20W PD fast charging chip, PD fast charging charger scheme
jenkins安装部署过程简记
Helping financial technology innovation and development, atfx is at the forefront of the industry
意派Epub360丨你想要的H5模板都在这里,电子书、大转盘、红包雨、问卷调查……
StickEngine-架构12-通信协议
Elasticsearch Part 6: aggregate statistical query
An article takes you to understand CSS gradient knowledge
JNI-Thread中start方法的呼叫與run方法的回撥分析
意外的元素..所需元素..
How to get started with new HTML5 (2)
Analysis of serilog source code -- how to use it
What if the front end doesn't use spa? - Hacker News
DRF JWT authentication module and self customization
From overseas to China, rancher wants to do research on container cloud market
What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online