《Beginning Python From Novice to Professional》学习笔记三:List
来源:互联网 发布:剑雨江湖进阶最新数据 编辑:程序博客网 时间:2024/04/27 02:07
1.删除元素
names = ['thy', 'jlj', 'god', 'dog']
del names[1] ---> ['thy', 'god', 'dog']
2.片段赋值
names[1:]=list('thy') ---> ['thy', 't', 'h', 'y']
names[2:2]=[] ---> ['thy', 't', 'y']
但要注意片段赋值的右值只能为List
3.append添加于尾部
names.append(3) ---> ['thy', 't', 'h', 'y', 3]
4.count统计指定元素
['to', 'be', 'or', 'not', 'to', 'be'].count('to') ---> 2
5.extend加入一个序列于尾部
gender = ['F', 'M']
names.extend(gender) ---> ['thy', 't', 'h', 'y', 3, 'M', 'F']
注意names + gender尽管结果相同,但它只是个缓存结果,除非names = names + gender
6.index返回第一个满足条件的下标
['to', 'be', 'or', 'not', 'to', 'be'].index('to') ---> 0
['to', 'be', 'or', 'not', 'to', 'be'].index('to', 1) ---> 4
7.insert于指定位置插入一个对象
names.insert(3, gender) ---> ['thy', 't', 'h', ['M', 'F'], 'y', 3, 'M', 'F']
注意此处是将gender看作一个整体List对象插入的
8.pop可以模拟栈操作(从尾部开始)
names.pop() ---> 'F' #(同时names的值变为['thy', 't', 'h', ['M', 'F'], 'y', 3, 'M'])
names.pop(3) ---> ['M', 'F'] #(同时names的值变为['thy', 't', 'h', 'y', 3, 'M'])
9.remove删除(但和pop不同,它不返回值)
10.reverse反转List
11.sort
对于基本类型很简单,默认为升序(sort(reverse=True)则接降序)
x = [4,6,2,1,7,9]
x.sort() ---> [1,2,4,6,7,9] #注意sort()不会返回值
names.sort() ---> [3, 'M', 'h', 't', 'thy', 'y']
y=x.sort()不会给y赋值,因为sort()不会返回值,可改用y=sorted(x)
对于复杂类型则需要指定cmp比较函数了(这放到函数一章)
还可以指定key来sort
x = ['dvark', 'abalone', 'acme', 'add', 'aerate']
x.sort(key=len) ---> ['add', 'acme', 'dvark', 'aerate', 'abalone']
x.sort() ---> ['abalone', 'acme', 'add', 'aerate', 'dvark']
names = ['thy', 'jlj', 'god', 'dog']
del names[1] ---> ['thy', 'god', 'dog']
2.片段赋值
names[1:]=list('thy') ---> ['thy', 't', 'h', 'y']
names[2:2]=[] ---> ['thy', 't', 'y']
但要注意片段赋值的右值只能为List
3.append添加于尾部
names.append(3) ---> ['thy', 't', 'h', 'y', 3]
4.count统计指定元素
['to', 'be', 'or', 'not', 'to', 'be'].count('to') ---> 2
5.extend加入一个序列于尾部
gender = ['F', 'M']
names.extend(gender) ---> ['thy', 't', 'h', 'y', 3, 'M', 'F']
注意names + gender尽管结果相同,但它只是个缓存结果,除非names = names + gender
6.index返回第一个满足条件的下标
['to', 'be', 'or', 'not', 'to', 'be'].index('to') ---> 0
['to', 'be', 'or', 'not', 'to', 'be'].index('to', 1) ---> 4
7.insert于指定位置插入一个对象
names.insert(3, gender) ---> ['thy', 't', 'h', ['M', 'F'], 'y', 3, 'M', 'F']
注意此处是将gender看作一个整体List对象插入的
8.pop可以模拟栈操作(从尾部开始)
names.pop() ---> 'F' #(同时names的值变为['thy', 't', 'h', ['M', 'F'], 'y', 3, 'M'])
names.pop(3) ---> ['M', 'F'] #(同时names的值变为['thy', 't', 'h', 'y', 3, 'M'])
9.remove删除(但和pop不同,它不返回值)
10.reverse反转List
11.sort
对于基本类型很简单,默认为升序(sort(reverse=True)则接降序)
x = [4,6,2,1,7,9]
x.sort() ---> [1,2,4,6,7,9] #注意sort()不会返回值
names.sort() ---> [3, 'M', 'h', 't', 'thy', 'y']
y=x.sort()不会给y赋值,因为sort()不会返回值,可改用y=sorted(x)
对于复杂类型则需要指定cmp比较函数了(这放到函数一章)
还可以指定key来sort
x = ['dvark', 'abalone', 'acme', 'add', 'aerate']
x.sort(key=len) ---> ['add', 'acme', 'dvark', 'aerate', 'abalone']
x.sort() ---> ['abalone', 'acme', 'add', 'aerate', 'dvark']
- 《Beginning Python From Novice to Professional》学习笔记三:List
- 《Beginning Python From Novice to Professional》学习笔记一:String
- 《Beginning Python From Novice to Professional》学习笔记二:Sequence
- 《Beginning Python From Novice to Professional》学习笔记四:Tuple
- 《Beginning Python From Novice to Professional》学习笔记六:Dictionary
- 《Beginning Python From Novice to Professional》学习笔记七:Statement
- 《Beginning Python From Novice to Professional》学习笔记八:Abstraction
- 《Beginning Python From Novice to Professional》学习笔记十:Exception
- 《Beginning Python From Novice to Professional》学习笔记十一:__Magic__
- 《Beginning Python From Novice to Professional》学习笔记十二:__Magic__
- Beginning Python: From Novice to Professional
- 《Beginning Python From Novice to Professional》学习笔记五:Advanced String
- 《Beginning Python From Novice to Professional》学习笔记九:More Abstraction
- Beginning Python From Novice to Professional (3) - 列表操作
- Beginning Python From Novice to Professional (6) - 函数使用
- Beginning Python From Novice to Professional (7) - 类
- Beginning Python From Novice to Professional (8) - 文件方法
- Beginning Python From Novice to Professional (9) - Socket
- informix 中创建DateTime类型字段
- ClickOnce的prerequisite组件安装报错
- 让LoadRunner走下神坛
- 回归RVDS
- 《Beginning Python From Novice to Professional》学习笔记二:Sequence
- 《Beginning Python From Novice to Professional》学习笔记三:List
- unbuntu倒计时
- 微软视频下载网站
- 一个决定
- 一个Hibernate 的例子,还有jsp上传和下载较为复杂的例子
- What Is Cygwin?---Cygwin全盘介绍(一)
- GET和POST的区别
- What Is Cygwin?---Cygwin全盘介绍(二)
- Asp.Net2.0验证视图状态 MAC 失败 ( Validation of viewstate MAC failed )-问题已解决