python 列表
来源:互联网 发布:android源码编译后rom 编辑:程序博客网 时间:2024/05/20 18:45
列表的定义:
# 定义一个空列表
list = [ ]
# 定义一个包含元素的列表,元素可以是任意类型,包括数值类型,列表,字符串等均可。
list = ["fentiao", 4, gender]
列表是可变类型的序列,而元组与字符串是不可变类型的序列
# 此处定义一列表,名为list1
>>> list1 = ["fentiao", 4, "male"]
# 通过python的内置函数type查看list1的数据类型
>>> type(list1)
<type 'list'>
# 此处我查看下标为1元素的值,重新给这个索引赋值,我们发现列表的元素是可变的;
>>> list1[1]
4
>>> list1[1] = 5
>>> list1
['fentiao', 5, 'male']
列表的操作:理解"对象=属性+方法"与类
列表的索引和切片
# 列表的索引:下标是从0开始计算,比如list[0]读取的是列表的第1个元素;
list[-1]读取的是列表的倒数第1个元素;
>>> list1[0]
'fentiao'
>>> list1[-1]
'male'
# 列表的切片
>>> list1[:] //通过切片的方式可以实现列表的复制
['fentiao', 5, 'male']
# 0代表从哪个索引开始切片,3代表切片到哪个位置,并且不包含第三个索引,2代表切片的步长;
>>> list1[0:3:2]
['fentiao', 'male']
列表的添加:
>>> list1[3] = "cat"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> list1.append('cat')
>>> list1
['fentiao', 5, 'male', 'cat']
列表的删除:
方法一:
>>> list1.remove("cat")>>> list1
['fendai', 5, 'male']
方法二:
>>> list1.remove(list1[3])
>>> list1
['fendai', 5, 'male']
方法三:
>>> del(list1[3])
>>> list1
['fendai', 5, 'male']
列表的修改
>>> list1
['fentiao', 5, 'male', 'cat']
>>> id(list1)
140205776057408
>>> list1[0] = "fendai"
>>> id(list1)
140205776057408
在指定位置添加元素使用inert方法:
- Python 列表
- python 列表
- python--列表
- python 列表
- Python 列表
- Python 列表
- python 列表
- Python列表
- python列表
- Python列表
- Python列表
- python列表
- Python列表
- python 列表
- python列表
- python列表
- Python--列表
- Python-列表
- [分块][哈希] [Romanian IOI 2017 Selection #6] Jolteon && [LOJ #6187] Odd
- IPV4须知
- Eclipse无法启动报An internal error occurred during: "reload maven project". java.lang.NullPointerExceptio
- 解决fatal error: bsoncxx/json.hpp: 没有那个文件或目录
- Arrays.binarySearch
- python 列表
- 使用c++11新特性实现线程池
- 微信支付类
- 最近比较忙
- js高级程序设计笔记——13章事件
- Linux 软件安装到 /usr,/usr/local/ 还是 /opt 目录?
- Gson笔记
- 学习shiro代码报错:Line argument must contain a key and a value. Only one string token was found.
- 牛客网_华为机试_005_进制转换