Python之列表(超详细)

列表是一种数据容器,用中括号"[ ]"表示。在一个列表中可以存放多个元素,这些元素的类型可以相同,也可以不同。在数据结构上,列表与C语言的数组类似,可以使用下标索引访问每一个元素。

一、创建列表

在Python中,可以通过一对中括号、切片及其他对象转换来创建列表,具体操作如下

tmp_list = [1,2,3.5+1j,0x22,"hello","world"]
print("tmp_list类型是:",type(tmp_list))
print("tmp_list元素是:",tmp_list)

tmp_list1=tmp_list[1:5]
print("tmp_list类型是:",type(tmp_list1))
print("tmp_list元素是:",tmp_list1)

tmp_list2=list(range(5))
print("tmp_list2类型是:",type(tmp_list2))
print("tmp_list2元素是:",tmp_list2)

列表的类型是" list ",执行结果如下

tmp_list类型是: <class 'list'>
tmp_list元素是: [1, 2, (3.5+1j), 34, 'hello', 'world']
tmp_list类型是: <class 'list'>
tmp_list元素是: [2, (3.5+1j), 34, 'hello']
tmp_list2类型是: <class 'list'>
tmp_list2元素是: [0, 1, 2, 3, 4]

二、修改和删除元素

要访问列表中的元素,可以通过对象名称加下标的形式,如下。列表的下标范围是[0,列表元素个数-1]。同时,列表是可变类型,修改和删除列表元素也不会重新创建对象。

tmp_list = [1,2,3.5+1j,0x22,"hello","world"]
print("获取指定元素:",tmp_list[2])
print("元素修改前:",id(tmp_list))
tmp_list[2]=88
print("元素修改后:",id(tmp_list))
print("修改元素后的列表:",tmp_list)
del tmp_list[2]
print("删除元素后的列表:",tmp_list)

执行结果如下

获取指定元素: (3.5+1j)
元素修改前: 2072266613504
元素修改后: 2072266613504
修改元素后的列表: [1, 2, 88, 34, 'hello', 'world']
删除元素后的列表: [1, 2, 34, 'hello', 'world']

三、列表迭代

在Python中,一个对象包含了_iter_和_getitem_函数,则称为可迭代对象,并可以用示例名[索引]的形式进行访问,同时还可以使用for循环进行遍历,如下

tmp_list = [1,2,3.5+1j,0x22,"hello","world"]
for item in tmp_list:
    print("当前元素是:",item)

执行结果如下

当前元素是: 1
当前元素是: 2
当前元素是: (3.5+1j)
当前元素是: 34
当前元素是: hello
当前元素是: world

四、列表切片

切片是Python中访问集合数据的高级用法,通过切片可以取得列表中指定范围的数据,具体用法如下

tmp_list = [1,2,3.5+1j,0x22,"hello","world"]
print("取出2~4范围数据:",tmp_list[1:4])
print("取出前3个数据:",tmp_list[:3])
print("取出第3个之后的数据:",tmp_list[3:])
print("在第1~6之间,每隔2个取一次:",tmp_list[1:6:2])
print("将列表反向输出:",tmp_list[::-1])

执行结果如下

取出2~4范围数据: [2, (3.5+1j), 34]
取出前3个数据: [1, 2, (3.5+1j)]
取出第3个之后的数据: [34, 'hello', 'world']
在第1~3之间,每隔2个取一次: [2, 34, 'world']
将列表反向输出: ['world', 'hello', 34, (3.5+1j), 2, 1]

五、列表加法

多个列表可以通过加法进行拼接,如下

tmp_list1=[1,2,3,4]
tmp_list2=[5,6,7,8]
tmp_list3=tmp_list1+tmp_list2
print("数据类型相同的列表相加:",tmp_list3)

tmp_list1=[1,2,3,4]
tmp_list2=["hello","world","python"]
tmp_list3=tmp_list1+tmp_list2
print("数据类型不同的列表相加:",tmp_list3)

执行结果如下,可以看到,列表相加并非列表元素两两相加,二十将两个列表拼接在一起,并返回新的列表。

数据类型相同的列表相加: [1, 2, 3, 4, 5, 6, 7, 8]
数据类型不同的列表相加: [1, 2, 3, 4, 'hello', 'world', 'python']

六、列表乘法

列表乘法是指列表与一个整数N相乘,这会将列表中的元素重复N次,并返回到一个新的列表,如下

tmp_list=[1,2,3]*3
print("整数列表*3:",tmp_list)

tmp_list=["hello","world"]*2
print("字符串列表*2:",tmp_list)

执行结果如下,输出乘法运算后的结果

整数列表*3: [1, 2, 3, 1, 2, 3, 1, 2, 3]
字符串列表*2: ['hello', 'world', 'hello', 'world']

七、列表推导

列表推导是Python中的高级用法,其原理和for循环类似,但是写法上更加简洁,并且有返回值,如下,在第2行,使用中括号来对列表tmp_list进行推导,语法格式为[变量for 变量 in 列表],列表推导执行完毕后,会将变量以列表形式返回。列表推导中还可以设置推导的条件,如"if item % 2 == 0"。因此,第2行代码的含义是对列表tmp_list进行推导,并返回其中的偶数。列表推导实际上可以使用for循环表示。第6行到第8行,就是第2行的循环写法。列表推导式也可以接外部函数。第18行,显示了如何在推导式中调用get_odd方法。

tmp_list=[1,2,3,4,5,6]
tmp_list1=[item for item in tmp_list if item %2==0]
print("使用推导式:",tmp_list1)

tmp_list2 = []
for item in tmp_list:
    if item % 2 == 0:
        tmp_list2.append(item)

print("使用for循环:",tmp_list2)

def get_odd(item):
    if item % 2 == 0:
        return  item
    else:
        return 0

tmp_list3=[get_odd(item)for item in tmp_list]
print("推导式调用外部方法:",tmp_list3)

执行结果如下

使用推导式: [2, 4, 6]
使用for循环: [2, 4, 6]
推导式调用外部方法: [0, 2, 0, 4, 0, 6]

八、列表函数

为方便操作列表对象,Python提供了多个函数。这些函数有的是给列表添加元素,有的是移除元素,有的是判断元素位置,有的是复制列表和统计元素个数。下面将详细演示如何使用这些内建函数。

1.append函数

append函数用于向列表末尾添加元素。该函数没有返回值,它只是修改了当前列表,如下

tmp_list=[1,2,3]
tmp_list.append(4)
print("列表元素:",tmp_list)

tmp_list=["hello","world"]
tmp_list.append("python")
print("列表元素:",tmp_list)

执行结果如下,输出列表

列表元素: [1, 2, 3, 4]
列表元素: ['hello', 'world', 'python']

2.insert函数

insert函数用于向列表中插入元素。该函数吗,没有返回值,而是直接将元素插入当前列表。insert函数的第一个参数是插入的位置,第二个参数是要插入的对象,如下

tmp_list=[1,2,3]
tmp_list.insert(2,10)
print("列表元素:",tmp_list)

tmp_list=["hello","world"]
tmp_list.insert(0,"python")
print("列表元素:",tmp_list)

执行结果如下

列表元素: [1, 2, 10, 3]
列表元素: ['python', 'hello', 'world']

3.extend函数

extend函数用于在列表的末尾添加另一个列表,与append函数相比,extend函数可以一次性添加多个元素,如下

a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print("输出列表a:", a)

执行结果如下,可以看到,使用extend函数和列表加法的结果是一样的,但是extend函数会将另一个列表并入当前列表,而列表加法是返回新的列表,为节约内存空间,更推荐使用extend函数来实现大列表的连接操作。

输出列表a: [1, 2, 3, 4, 5, 6]

4.remove函数

remove函数用于从列表移除元素。该函数没有返回值,直接操作原列表,如下

a=[1,2,3]
a.remove(3)
print("输出列表a:",a)

a=["hello","world","python","hello"]
a.remove("hello")
print("输出列表a:",a)

执行结果如下,可以看到,若列表中有重复元素,remove函数只会移除匹配到的第一个。

输出列表a: [1, 2]
输出列表a: ['world', 'python', 'hello']

5.pop函数

pop函数用于移除列表中指定位置的元素,并返回要移除的元素。在默认情况下,移出列表中最后一个元素,如下

a=["hello","world","python","hello"]
elem=a.pop(1)
print("移除的元素是:",elem)
print("移除元素后的列表:",a)

执行结果如下,输出移除的元素和移除元素后的列表

移除的元素是: world
移除元素后的列表: ['hello', 'python', 'hello']

6.clear函数

clear函数用于将列表清空,如下

a=["hello","world","python","hello"]
a.clear()

print("输出清空后的列表:",a)
print("输出清空后的列表长度0:",len(a))

执行结果如下,可以看到清空后的列表为[ ],长度为0。

输出清空后的列表: []
输出清空后的列表长度0: 0

7.reverse函数

reverse函数用于将列表反向排列,如下

a=["hello","world","python","hello"]
a.reverse()
print("输出反向排列的列表:",a)

执行结果如下,可以看到reverse函数将列表进行反向排列

输出反向排列的列表: ['hello', 'python', 'world', 'hello']

8.sort函数

sort函数用于将列表进行排序。如下,在默认情况下,sort函数会将列表进行升序排列。在第6行制定了参数reverse=True,对列表进行降序排列,在第13行,待排序的列表元素类型是字典,需要根据字典的键来排序,就要使用key参数。key参数后面是一个lambda表达式,这是一个函数对象,该函数对象返回了item[“price”],那么sort函数就会使用item[“price”]来进行排序。

a=["hello","world","hello","python"]
a.sort()
print("将列表进行升序排序:",a)

a=[1,9,6,7]
a.sort(reverse=True)
print("对列表进行降序排序:",a)

a=[{"price":10.5},
  {"price":24},
  {"price":15}]

a.sort(key=lambda item:item["price"],reverse=True)
print("对列表中数据按指定条件进行降序排序:\n",a)

执行结果如下,输出排序后的列表

将列表进行升序排序: ['hello', 'hello', 'python', 'world']
对列表进行降序排序: [9, 7, 6, 1]
对列表中数据按指定条件进行降序排序:
 [{'price': 24}, {'price': 15}, {'price': 10.5}]

9.copy函数

copy函数用于创建列表的副本。注意,创建副本和赋值是不一样的,如下,在第2行,调用copy函数并赋值给b,那么b就是a的副本。在第9行,将c赋值给d,那么d是c的引用

a=["hello","world","hello","python"]
b=a.copy()
print("创建a的一个副本并赋值给b:",b)

del a[0]
print("删除a[0]元素后,在输出b:",b)

c=["hello","world","hello","python"]
d=c
del c[0]
print("删除c[0]元素后,再输出d:",d)

执行结果如下,可以看到,删除a列表元素并未对其副本b造成影响。删除c列表元素,会影响对应的引用b

创建a的一个副本并赋值给b: ['hello', 'world', 'hello', 'python']
删除a[0]元素后,在输出b: ['hello', 'world', 'hello', 'python']
删除c[0]元素后,再输出d: ['world', 'hello', 'python']

10.index函数

index函数用于返回所匹配的元素的索引。该函数的第一个参数是待查找的对象,第二个参数是查找的起始范围,第三个参数是查找的结束范围,如下

a=["hello","world","hello","python"]
result=a.index("hello")
print("hello在列表中的索引位置:",result)

result=a.index("hello",1,3)
print("hello在列表中的索引位置:",result)

可以看到,列表中的重复元素" hello ",在没有指定查询范围时,index函数返回匹配到的第一个索引位置。若是指定了范围,则返回范围内匹配到的第一个索引位置。执行结果如下

hello在列表中的索引位置: 0
hello在列表中的索引位置: 2

11.count函数

count函数用于统计某个元素在列表中出现的次数,如下

a=["hello","world","hello","python"]
result=a.count("world")
print("world在列表中的个数:",result)

result=a.count("hello")
print("hello在列表中的个数:",result)

执行结果如下,输出元素的个数

world在列表中的个数: 1
hello在列表中的个数: 2

本篇文章就到这里结束了,希望可以给小伙伴们一些帮助,喜欢的小伙伴们可以点赞关注支持一下!!

来源:8X_I

物联沃分享整理
物联沃-IOTWORD物联网 » Python之列表(超详细)

发表评论