1. 列表介绍
序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。Python有6个序列的内置类型,但最常见的是列表和元组。
序列都可以进行的操作包括索引,切片,加,乘,检查成员;
此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法;
列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现;
列表的数据项不需要具有相同的类型。
创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]
与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等。
2. 访问列表中的值
使用下标索引来访问列表中的值,同样你也可以使用方括号的形式截取字符,如下所示:
访问list1第一个元素: physics
截取list2,从索引1-5 [2, 3, 4, 5]
倒序list1: [2000, 1997, 'chemistry', 'physics']
访问list2的最后一个元素: 7
截取list1除了第一个元素: ['chemistry', 1997, 2000]
输出结果:
列表中也可以嵌套列表,如下所示:
list=[['http',80],['ftp',21],['ssh',22]]
print(list[0][1]) ##访问列表中第一个子列表中的第二个元素
print(list[:][1]) ##截取第二个子列表
运行结果:
3. 更新列表
你可以对列表的数据项进行修改或更新,你也可以使用append()方法来添加列表项,如下所示:
list = [] ## 空列表
list.append('Google') ## 使用 append() 添加元素
list.append('Runoob')
print (list)
运行结果:
可以使用 del 语句来删除列表的元素,如下实例:
print(list1)
del list1[2]
print('After deleting value at index 2 : ',list1)
运行结果:
4. 列表操作符
列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。
如下所示:
Python 表达式 | 结果 | 描述 |
---|---|---|
len([1, 2, 3]) | 3 | 长度 |
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | 组合 |
[‘Hi!’] * 4 | [‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’] | 重复 |
3 in [1, 2, 3] | True | 元素是否存在于列表中 |
for x in [1, 2, 3]: print x, | 1 2 3 | 迭代 |
5. Python列表函数&方法
函数 | 作用 |
---|---|
max(list) | max() 方法返回列表元素中的最大值 |
min(list) | 返回列表元素最小值 |
list(seq) | 将元祖转换为列表 |
list.append(obj) | 在列表末尾添加新的对象 |
list.count(obj) | 统计某个元素在列表中出现的次数 |
list.extend(seq) | 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) |
list.index(obj) | 从列表中找出某个值第一个匹配项的索引位置 |
list.insert(index, obj) | 将对象插入列表 |
list.pop([index=-1]) | 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 |
list.remove(obj) | 移除列表中某个值的第一个匹配项 |
list.reverse() | 反向列表中元素 |
list.sort(cmp=None, key=None, reverse=False) | 对原列表进行排序 |
list.clear() | 清空列表 |
list.copy() | 复制列表 |
实例:
list1, list2 = ['123', 'xyz', 'zara', 'abc'], [456, 700, 200]
print("Max value element : ", max(list1))
print("Max value element : ", max(list2))
print("min value element : ", min(list1))
print("min value element : ", min(list2))
aTuple = (123, 'xyz', 'zara', 'abc')
aList = list(aTuple)
print("列表元素 : ", aList)
aList1 = [123, 'xyz', 'zara', 'abc'];
aList1.append( 123 )
print("Updated List : ", aList1)
print("Count for 123 : ", aList1.count(123))
print("Count for zara : ", aList1.count('zara'))
bList = [2009, 'manni']
aList1.extend(bList)
print ("Extended List : ", aList1)
cList = [123, 'xyz', 'runoob', 'abc','haha','runoob']
print("xyz 索引位置: ", cList.index( 'xyz' ))
print("指定位置内的runoob 索引位置 : ", cList.index( 'runoob',3,6))
cList.insert(3, 2009)
print("Final List : ", cList)
dList = ['Google', 'Runoob', 'Taobao']
list_pop=dList.pop(1)
print("删除的项为 :", list_pop)
print("列表现在为 : ", dList)
eList = [123, 'xyz', 'zara', 'abc', 'xyz'];
eList.remove('xyz');
print("eList : ", eList)
eList.remove('abc')
print("eList : ", eList)
eList.reverse()
print("反向eList:",eList)
fList = ['Google', 'Runoob', 'Taobao', 'Facebook']
fList.sort()
print("fList : ", fList)
vowels = ['e', 'a', 'u', 'o', 'i']
# 降序
vowels.sort(reverse=True)
# 输出结果
print('降序输出:', vowels)
#升序
vowels.sort(reverse=False)
print('升序输出:', vowels)
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
# 获取列表的第二个元素
def takeSecond(elem):
return elem[1]
# 列表
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
# 指定第二个元素排序
random.sort(key=takeSecond),
print('排序列表:', random)
flist = ['Google', 'Runoob', 'Taobao', 'Baidu']
glist = flist.copy()
print ("glist列表: ", glist)
flist.clear()
print ("f列表清空后 : ", flist)