Python list
来源:互联网 发布:手机科研数据处理软件 编辑:程序博客网 时间:2024/05/21 09:40
List
1、List 可以包含不同类型的 item,但 item 通常有相同的类型
>>> squares = [1, 2, 4, 9, 16, 25]
>>> squares
[1, 2, 4, 9, 16, 25]
2、像其他内置序列类型一样,List 也可以被 索引(index) 和 切片(slice)
3、所有的 slice 操作,都会产生一个新的 list;
4、list 也支持拼接操作
>>> squares + [36, 49, 64, 81, 100]
[1, 2, 4, 9, 16, 25, 36, 49, 64, 81, 100]
5、不同于 string 的是,list 的元素是可以改变的
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here
>>> 4 ** 3 # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
6、可以使用 append() 函数,来给 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]
7、list 的 片段 也是可以改变的,可以通过这个方法改变 list 的大小 或者清空
>>> 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
[]
8、len() 函数也用于计算 list 的大小
9、lists 的嵌套也是可以的
>>> 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'
others:
1、多变量赋值:
a,b = 0,1 # 表示:a = 0, b = 1
2、在 while 循环中,非 0 值整数 为 true,0 值表示 false;序列也可以作为条件值,所有序列中,非 0 长度是 true,空序列为 false
3、循环语句的循环体是缩进的。Python 使用缩进来组织语句。你可以使用 tab 键或者 空格键 来缩进。属于同一个基本块的语句行要有相同的缩进数量
4、print() 函数会自动格式化输出的 item
>>> i = 256*256
>>> print('The value of i is', i)
The value of i is 65536
为了在序列化输出中指定分割符,可以使用 end 参数
>>> a, b = 0, 1
>>> while b < 1000:
... print(b, end=',')
... a, b = b, a+b
...
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,>>>
- 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
- 漫谈varchar和Nvarchar区别
- DB2 linux 图形界面 安装
- 回调函数
- linux 引导流程
- HTML
- Python list
- 2014.2.16学习笔记
- 用java调用oracle存储过程总结
- 多幅TM影像的处理技术总结
- php+mysql+jquery瀑布流
- android触摸屏控制
- 二叉树遍历顺序
- 计算机等级考试
- 关于C#中垃圾回收GC杂谈