python天天练120510
Email: zcatt@163.com
Blog http://zcatt.blog.chinaunix.net
声明
仅限学习交流,禁止商业用途。转载需注明出处。
版本记录
Date Ver Note
2012-05-10 0.1 Draft. zcatt, Beijing
*. List
=================================
list由方括号包围,逗号间隔的一组数据组成. list中的数据这里称作元素.
list的特征,
1)一个list中, 各元素的类型可以不同
2)各元素有序的, list支持slice
3)list中的元素是mutable, 即可改变的.
**.slice操作
============
- >>> a=[1,2,3,4]
- >>> a
- [1, 2, 3, 4]
- >>> a[0]
- 1
- >>> a[3]
- 4
- >>> a[-1]
- 4
- >>> a[1:2]
- [2]
- >>> a[1:3]
- [2, 3]
- >>>
**.插入和删除
=============
- >>> a=[1,2,3,4]
- >>> a[1:1]=['a','b']
- >>> a
- [1, 'a', 'b', 2, 3, 4]
- >>> a[1:3]=[]
- >>> a
- [1, 2, 3, 4]
**.头加和尾追
=============
- >>> a
- [1, 2, 3, 4]
- >>> a[:0]=['a']
- >>> a
- ['a', 1, 2, 3, 4]
- >>> a[-1:]=['e']
- >>> a
- ['a', 1, 2, 3, 'e']
- >>>
**.引用和复制
=============
引用
- >>> a=[1,2,3,4]
- >>> a
- [1, 2, 3, 4]
- >>> b=a
- >>> b
- [1, 2, 3, 4]
- >>> a[1]='m'
- >>> a
- [1, 'm', 3, 4]
- >>> b
- [1, 'm', 3, 4]
- >>>
复制
- >>> a=[1,2,3,4]
- >>> a
- [1, 2, 3, 4]
- >>> b=a[:]
- >>> b
- [1, 2, 3, 4]
- >>> a[1]='m'
- >>> a
- [1, 'm', 3, 4]
- >>> b
- [1, 2, 3, 4]
- >>>
**.len
=======
- >>> a=[1,2,3,4]
- >>> len(a)
- 4
- >>>
阅读(560) | 评论(0) | 转发(0) |