python list
来源:互联网 发布:分贝测试软件 编辑:程序博客网 时间:2024/05/18 02:35
一、空的list
s = [] 或者 s = list()
二、list能索引和切片,能根据索引和切片直接修改原来的值
squares=[1,4,9,16,25]
>>> squares[0] # indexing returns the item1>>> squares[-1]25>>> squares[-3:] # slicing returns a new list[9, 16, 25]
>>> cubes = [1, 8, 27, 65, 125] #根据索引修改>>> cubes[3] = 64 >>> cubes[1, 8, 27, 64, 125]
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']>>> letters['a', 'b', 'c', 'd', 'e', 'f', 'g']>>> # replace some values>>> letters[2:5] = ['C', 'D', 'E'] #根据切片修改>>> letters['a', 'b', 'C', 'D', 'E', 'f', 'g']>>> # now remove them>>> letters[2:5] = []>>> letters['a', 'b', 'f', 'g']>>> # clear the list by replacing all the elements with an empty list>>> letters[:] = []>>> letters[]
三、list支持连接
>>> squares = squares + [36, 49, 64, 81, 100]
>>> squares
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
四、list插入值到末尾
>>> cubes.append(216) # add the cube of 6
>>> cubes.append(7 ** 3) # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
五、获取list长度
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
六、嵌套list
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
七、复制list
已有一个列表a,若b=a,则b是a的引用,修改b也会修改a;若是b=a[:],则b的修改不会影响a
八、list相关的方法
a.append(1),将1添加到列表a的末尾
a.count(1),统计列表a中元素1出现的次数
a.extend([1,2]),将列表[1,2]的内容追加到列表a的末尾
a.index(1),从列表a中找出第一个1的索引位置
a.insert(2,1),将1插入列表a的索引为2的位置
a.pop(1),移除列表a中索引为1的元素
a.remove(1),移除列表a中值为1的第一个元素
a.clear(),清空列表
a.sort(),排序
a.reverse(),列表逆序
del a[0],删除列表a索引为0的元素
del a[2:4],删除列表a的索引2到4(不包括4)的元素
del a[:],清空列表a
- Python List:
- python list
- python list
- python list
- Python.list
- Python list
- python list
- Python List
- Python List
- python list
- Python---list
- python-list
- python list
- python list
- python list
- Python List
- Python list
- python list
- C#判断WebBrowser加载完毕的方法
- 创建存储过程和调用存储过程(Mysql)
- Spark RDD概念学习系列之Spark的算子的分类(十一)
- 年会系统-心得体会
- Lambda表达式详解
- python list
- 织梦系统(DEDECMS)后台模板修改
- 微信小程序 Video API
- 骚然拦截的一些实现
- 自动驾驶全球布局(1)互联网公司
- 405. Convert a Number to Hexadecimal#2
- 微信小程序 Storage API
- java,scala之spark streaming 版本的单词统计(通过监听端口)
- 2016——再见,再也不见