列表
- 序列数据 声明
- `list1 = [1,2,4,"heeoo",[2,3,4]`
- 索引 index : 0开始, 用list1[0] 去访问
- 如果说, 索引值超出了范围(len() - 1), 则报一个 IndexError: list index out of range
#!/usr/bin/python
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]
以上实例输出结果:
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
- 长度: len()
- 访问最后一个元素 list1[len(list1) - 1]
- min
- max
- 运算符
- 、+ 合并列表
list = [1,2,3]
list1 = [4,5,6]
print(list + list1)
#[1, 2, 3, 4, 5, 6]
- 、* 重复数次
list=['Hi!']
print(list * 4)
#['Hi!', 'Hi!', 'Hi!', 'Hi!']
- 遍历列表中的所有元素: for in
for list in list1: #遍历list1列表
- 判断是否存在
- in
- not in
# -*- coding:utf-8 -*-
#list去重
list1 = ['physics', 'chemistry',"sss",2019,1997,"sss","physics", 1997, 2000,100,2019]
list2 = []
for list in list1:
if list not in list2:
list2.append(list)
print(list2)
切片
- list1[start🔚step]
- start 默认从0开始, -1 代表倒数第一个,
- end 默认为len(list1) , 不包含end
- step 默认为1 , 可以正数, 也可以负数
- 列表倒序: list1[-1::-1])
number = [0,1,2,3,4,5,6,7,8,9]#
print(number[10:0:-2])
学习函数
- 方法的名称
- 方法的参数
- 方法的返回值
列表的方法
- 新增:
- append(obj) 添加到列表末尾的对象。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
list = [] ## 空列表
list.append('Google') ## 使用 append() 添加元素
list.append('Runoob')
print list
#注意:我们会在接下来的章节讨论append()方法的使用
#以下为输出结果:
['Google', 'Runoob']
- extend(seq) 用于在列表末尾一次性追加另一个序列中的多个值
#!/usr/bin/python
aList = [123, 'xyz', 'zara', 'abc', 123];
bList = [2009, 'manni'];
aList.extend(bList)
print "Extended List : ", aList ;
#以上实例输出结果如下:
Extended List : [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']
- insert
- index 索引位置
- obj 插入的对象
#!/usr/bin/python
aList = [123, 'xyz', 'zara', 'abc']
aList.insert( 3, 2009)
print "Final List : ", aList
#以上实例输出结果如下:
Final List : [123, 'xyz', 'zara', 2009, 'abc']
- 删除元素
- pop
- 索引值, 默认是-1
- 返回值为被删除的元素
- pop
#!/usr/bin/python3
#coding=utf-8
list1 = ['Google', 'Runoob', 'Taobao']
list_pop=list1.pop(1)
print "删除的项为 :", list_pop
print "列表现在为 : ", list1
#以上实例输出结果如下:
删除的项为 : Runoob
列表现在为 : ['Google', 'Taobao']
-remove 用于移除列表中某个值的第一个匹配项。
- 要移除的元素
- 如果找不到该元素,则报异常
-
#!/usr/bin/python
aList = [123, 'xyz', 'zara', 'abc', 'xyz'];
aList.remove('xyz');
print "List : ", aList;
aList.remove('abc');
print "List : ", aList;
#以上实例输出结果如下:
List : [123, 'zara', 'abc', 'xyz']
List : [123, 'zara', 'xyz']
- 查找元素
- count
- obj 统计某个元素在列表中出现的次数
- count
#!/usr/bin/python
aList = [123, 'xyz', 'zara', 'abc', 123];
print "Count for 123 : ", aList.count(123);
print "Count for zara : ", aList.count('zara');
#以上实例输出结果如下:
Count for 123 : 2
Count for zara : 1
index
- obj 查找的对象
- start 包含start
- end 不包含end
#!/usr/bin/python
# -*- coding: UTF-8 -*-
aList = [123, 'xyz', 'runoob', 'abc']
print "xyz 索引位置: ", aList.index( 'xyz' )
print "runoob 索引位置 : ", aList.index( 'runoob', 1, 3 )
#以上实例输出结果如下:
xyz 索引位置: 1
runoob 索引位置 : 2
- 改顺序
- reverse 反向列表中元素 ,把原来列表改变
#!/usr/bin/python
aList = [123, 'xyz', 'zara', 'abc', 'xyz']
aList.reverse()
print "List : ", aList
#以上实例输出结果如下:
List : ['xyz', 'abc', 'zara', 'xyz', 123]
- sort 排序 list3.sort(reverse=True)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
aList = [123, 'Google', 'Runoob', 'Taobao', 'Facebook'];
aList.sort();
print "List : ", aList
#以上实例输出结果如下:
List : [123, 'Facebook', 'Google', 'Runoob', 'Taobao']
#以下实例降序输出列表:
#实例2
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 列表
vowels = ['e', 'a', 'u', 'o', 'i']
# 降序
vowels.sort(reverse=True)
# 输出结果
print '降序输出:', vowels