天天看点

Python编程基础:列表及序列基本操作(二)

作者:青少年编程CodeTeen

序列的加法运算

同一类型的序列可以进行加法运算,实现两个序列合并为一个序列。

list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = list1 + list2
print(list3) #结果:[1, 2, 3, 4, 5, 6]           

序列的乘法运算

序列和一个正整数n相乘,可以将序列重复n次并合并为新的序列。

list1 = ['a', 'b', 'c']
list2 = list1 * 2
print(list2) #结果:['a', 'b', 'c', 'a', 'b', 'c']           

列表的专用操作

一、更新列表中元素的值

列表是可变序列,元素的值是可以修改的。

list1 = [1, 2, 3]
list1[1] = 5 #第二个元素值改为5
print(list1) #结果:[1, 5, 3]           

也可以同时修改多个元素的值,使用切片获取要修改的多个元素。

list1 = [1, 2, 3, 4, 5, 6]
list1[1:4] = [7, 8, 9]
print(list1) #结果:[1, 7, 8, 9, 5, 6]           

二、删除列表元素

删除列表的元素可以有以下多种方法。

  1. del命令
list1 = [1, 2, 3, 4, 5, 6]
#删除指定的元素
del list1[1] #删除第二个元素
print(list1) #结果:[1, 3, 4, 5, 6]

del list1[:2] #删除前2个元素
print(list1) #结果:[4, 5, 6]

del list1 #删除整个列表
print(list1) #列表已经删除,会报错:NameError: name 'list1' is not defined           
  1. pop()方法

列表的pop()方法可以删除指定索引的元素,如果未指定索引,则删除最后一个元素。

list1 = [1, 2, 3, 4, 5, 6]
list1.pop(0) #删除第一个元素 
print(list1) #结果:[2, 3, 4, 5, 6]

list1.pop() #删除最后一个元素 
print(list1) #结果:[2, 3, 4, 5]           
  1. remove()方法

列表的remove()方法可以删除指定值的第一个元素。如果值不在列表中,会报错。

list1 = [1, 2, 3,1, 5, 6]
list1.remove(1) #删除列表中第一个1
print(list1) #结果:[2, 3, 1, 5, 6]

list1.remove(9) #删除列表中的9,由于不包含中列表中,会报错:ValueError: list.remove(x): x not in list

#为了程序不出现错误,可以在remove之前增加值是否在列表中的判断
if 9 in list1:
    list1.remove(9)
    print(list1)
else:
    print('list1不包含9') #结果:list1不包含9           
  1. clear()方法

列表的clear()方法可以清除列表中的所有元素,列表变成空列表。

list1 = [1, 2, 3,1, 5, 6]
list1.clear()
print(list1) #结果:[]           

三、增加元素

可以在列表后面新加一个元素,也可以在任意位置插入一个元素。

  1. 使用append()方法在列表后增加元素

格式:list_name.append(item)

说明:list_name是列表的名称;item是要增加的元素,可以是数字、字符串,也可以是列表、元组等。

list1 = [1, 2, 3]
list1.append(4)
print(list1) #结果:[1, 2, 3, 4]

list1.append([5, 6, 7])
print(list1) #结果:[1, 2, 3, 4, [5, 6, 7]]           
  1. 使用insert()方法在列表指定位置插入元素

格式:list_name.insert(index, item)

说明:list_name是列表的名称;index是索引,item是要插入的元素。index如果超出列表的索引最大值,并不会出错,而是在列表最后增加元素。

list1 = [1, 2, 3]
list1.insert(0, 8)
print(list1) #结果:[8, 1, 2, 3]

list1.insert(3, 9)
print(list1) #结果:[8, 1, 2, 9, 3]

list1.insert(15, 10)
print(list1) #结果:[8, 1, 2, 9, 3, 10]           

四、查找元素

  1. 使用index()查找元素在列表中的位置,返回元素的索引

格式:list_name.index(item, start, end)

说明:list_name是列表的名称;item是要查找的内容;start是开始位置索引;end是结束位置索引。start和end可以不提供,代表在整个列表中查找。

列表中如果有多个元素匹配,返回的是查找到的第一个元素的索引。

如果item不包含在列表中,则会发生ValueError错误。

list1 = list('python')
i = list1.index('y')
print(i) #结果:1

list1.index('z') #ValueError: 'z' is not in list           
  1. 使用count()统计元素在列表中的数量

格式:list_name.count(item)

说明:统计item在列表list_name中出现的次数,如果列表中找不到item,则返回0

list1 = list('hello')
cnt = list1.count('l')
print(cnt) #结果:2
print(list1.count('a')) #结果:0           

五、列表元素的排序

  1. 使用sort()方法对列表进行排序

格式:list_name.sort(cmp=None, key=None, reverse= False)

说明:sort()方法用于对列表进行排序,直接修改原列表,不返回新的列表,返回值为None。默认是升序排序,如果要降序排序,可以设置reverse为True。

list1 = list('ahobek')
list1.sort()
print(list1) #结果:['a', 'b', 'e', 'h', 'k', 'o']

list1.sort(reverse=True) #降序排序
print(list1) #结果:['o', 'k', 'h', 'e', 'b', 'a']           
  1. 使用sorted()内置函数对列表进行排序

sorted()是内置函数,可以对包括列表在内的序列进行排序,会返回新的列表,不改变原序列。

格式:sorted(iterable, cmp=None, key=None, reverse=False)

说明:iterable是可迭代对象(如列表等序列);reverse为True时代表降序排序。

list1 = [1, 4, 2, 10, 54, 18]
list2 = sorted(list1)
print(list2) #结果:[1, 2, 4, 10, 18, 54]

list3 = sorted(list1, reverse=1) #降序排序
print(list3)  #结果:[54, 18, 10, 4, 2, 1]           
  1. 使用reverse()方法对列表的元素前后颠倒

格式:list_name.reverse()

reverse()方法并不对列表进行排序,只是颠倒元素,即第一个元素调到最后一个,第二个调到倒数第二个,以此类推。

reverse()在原列表中操作,不返回新的列表,返回值为None。

list1 = [1, 4, 2, 10, 54, 18]
list1.reverse()
print(list1) #结果:[18, 54, 10, 2, 4, 1]