当前位置:网站首页>Python基础变量类型——List浅析
Python基础变量类型——List浅析
2020-11-06 20:53:00 【Python进阶者】
Python使用list
一、list
Python内置的一种数据类型是列表:list。list是一种有序的集合,可以随时添加和删除其中的元素。
比如,列出班里所有同学的名字,就可以用一个list表示:
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates)

变量classmates就是一个list。
len()函数
1. 获得list元素的个数:
classmates = ['Michael', 'Bob', 'Tracy']
print(len(classmates))

用索引来访问list中每一个位置的元素,记得索引是从0开始的:
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates[0])
print(classmates[1])
print(classmates[2])
print(classmates[3])

当索引超出了范围时,Python会报一个IndexError错误,所以,要确保索引不要越界,记得最后一个元素的索引是len(classmates) - 1。
如果要取最后一个元素,除了计算索引位置外,还可以用-1做索引,直接获取最后一个元素:
print(classmates[-1])

以此类推,可以获取倒数第2个、倒数第3个:
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates[-1])
print(classmates[-2])
print(classmates[-3])
print(classmates[-4])

当然,倒数第4个就越界了。
2. list是一个可变的有序表,往list中追加元素到末尾:
classmates = ['Michael', 'Bob', 'Tracy']
classmates.append('Adam')
print(classmates)
也可以把元素插入到指定的位置,比如索引号为1的位置:
classmates = ['Michael', 'Bob', 'Tracy']
#替换
classmates.insert(1, 'Jack')
print(classmates)

pop()函数
1. 删除list末尾的元素
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates.pop())
print( classmates)
['Michael', 'Jack', 'Bob', 'Tracy']

2. 删除指定位置的元素,用pop(i)方法,其中i是索引位置。
classmates.pop(1)
print(classmates)

3. 把某个元素替换成别的元素,可以直接赋值给对应的索引位置:
classmates = ['Michael', 'Bob', 'Tracy']
classmates[1] = 'Sarah'
print(classmates)

list里面的元素的数据类型也可以不同,比如:
L = ['Apple', 123, True]
list元素也可以是另一个list,比如:
s = ['python', 'java', ['asp', 'php'], 'scheme']
print(len(s))

要注意s只有4个元素,其中s[2]又是一个list,如果拆开写就更容易理解了:
p = ['asp', 'php']
s = ['python', 'java', p, 'scheme']
要拿到'php'可以写p[1]或者s[2][1],因此s可以看成是一个二维数组,类似的还有三维、四维……数组,不过很少用到。
如果一个list中一个元素也没有,就是一个空的list,它的长度为0:
L = []
len(L)
二、总结
本文基于Python基础,主要介绍了Python基础中list列表,通过list列表的两个函数 ,对list的语法做了详细的讲解,用丰富的案例 ,代码效果图的展示帮助大家更好理解 。
使用Python编程语言,方便大家更好理解,希望对大家的学习有帮助。 想学习更多Python网络爬虫与数据挖掘知识,可前往专业网站:http://pdcfighting.com/ 想学习更多Python网络爬虫与数据挖掘知识,可前往专业网站:http://pdcfighting.com/
版权声明
本文为[Python进阶者]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4521128/blog/4671334
边栏推荐
- The road of C + + Learning: from introduction to mastery
- From zero learning artificial intelligence, open the road of career planning!
- Swagger 3.0 天天刷屏,真的香嗎?
- I'm afraid that the spread sequence calculation of arbitrage strategy is not as simple as you think
- It's so embarrassing, fans broke ten thousand, used for a year!
- Just now, I popularized two unique skills of login to Xuemei
- What is the side effect free method? How to name it? - Mario
- Can't be asked again! Reentrantlock source code, drawing a look together!
- 至联云分享:IPFS/Filecoin值不值得投资?
- Do not understand UML class diagram? Take a look at this edition of rural love class diagram, a learn!
猜你喜欢

Network security engineer Demo: the original * * is to get your computer administrator rights! 【***】

Summary of common algorithms of binary tree

Filecoin最新动态 完成重大升级 已实现四大项目进展!

加速「全民直播」洪流,如何攻克延时、卡顿、高并发难题?

Not long after graduation, he earned 20000 yuan from private work!

Troubleshooting and summary of JVM Metaspace memory overflow

熬夜总结了报表自动化、数据可视化和挖掘的要点,和你想的不一样

Tool class under JUC package, its name is locksupport! Did you make it?

Brief introduction of TF flags

在大规模 Kubernetes 集群上实现高 SLO 的方法
随机推荐
一篇文章带你了解CSS3圆角知识
NLP model Bert: from introduction to mastery (2)
From zero learning artificial intelligence, open the road of career planning!
ipfs正舵者Filecoin落地正当时 FIL币价格破千来了
TRON智能钱包PHP开发包【零TRX归集】
Skywalking series blog 1 - install stand-alone skywalking
6.3 handlerexceptionresolver exception handling (in-depth analysis of SSM and project practice)
带你学习ES5中新增的方法
ES6学习笔记(二):教你玩转类的继承和类的对象
关于Kubernetes 与 OAM 构建统一、标准化的应用管理平台知识!(附网盘链接)
Installing the consult cluster
熬夜总结了报表自动化、数据可视化和挖掘的要点,和你想的不一样
Filecoin最新动态 完成重大升级 已实现四大项目进展!
如何玩转sortablejs-vuedraggable实现表单嵌套拖拽功能
Keyboard entry lottery random draw
What is the difference between data scientists and machine learning engineers? - kdnuggets
6.5 request to view name translator (in-depth analysis of SSM and project practice)
在大规模 Kubernetes 集群上实现高 SLO 的方法
6.1.2 handlermapping mapping processor (2) (in-depth analysis of SSM and project practice)
Leetcode's ransom letter