0%

Python列表、字典、元组和集合

列表

列表由一系列按特定顺序排列的元素组成。在python中,用方括号[]来表示列表,并用逗号来分割其中的元素。

列表有如下函数可以使用,可以通过dir(list)查看所有的函数:

方法 描述
append(x) 在列表末尾添加元素x
clear() 移除列表中的所有元素
copy() 复制列表,浅复制
count(x) 返回在值为x的元素个数
extend(iterable) 将可迭代对象的元素添加到列表末尾
index(x[,start[,end]]) 返回第一个值为x的元素的索引,可选参数start和end用于指定搜索范围。注:start的位置会搜索,end的位置不会搜索
insert(i,x) 在指定位置i插入元素x
pop([i]) 移除并返回指定位置i的元素,如果没有指定位置,则默认移除并返回最后一个元素
remove(x) 移除列表中第一个值为x的元素
reverse() 对列表中元素顺序反转,直接在原列表上进行
sort(key=None,reverse=False) 对列表进行排序,可选参数key用于指定排序的函数,reverse用于指定排序的顺序

列表的创建

1
2
3
4
5
6
7
8
9
10
# 列表中元素的类型可以是多种多样,包括数字、字符、字符串等等
list_temp1 = ['1','abc','ABC','']
# 列表中的元素也可以是列表、元组、字典等,例如
list_temp2 = ['1',(1,2,3),{1:'a',2:'b'}]
# 访问列表中的元素,注意,列表中的索引从0开始
print(list_temp1[0]) # 1
print(list_temp2[1]) # (1,2,3)
# 修改列表中的元素
list_temp1[0] = 2
print(list_temp[0]) # 2

sort和sorted的区别

sort方法是列表的一个内部方法,它直接在原列表上进行排序,修改原列表。sorted是一个内置函数,可以对所有可迭代的对象进行排序,并返回一个新的已排序的列表。这意味着它不会修改原始数据,而是返回一个新的排序后的列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# sort的使用
list_sort1 = [1,'a','c']
# 列表中的元素类型不同时,会抛出异常
list_sort1.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'
# sort函数永久性排序
list_sort2 = ['a','d','fed','aef']
list_sort2.sort()
print(list_sort2) # ['a', 'aef', 'd', 'fed']
# 指定排序函数
list_sort3 = ['ca','sd','feq','rb']
list_sort3.sort(key=lambda x:x[1]) # 根据每个元素中的第二个字符进行排序
print(list_sort3) # ['ca', 'rb', 'sd', 'feq']

# sorted的使用
list_sort4 = [1,2,5,4,3]
print(sorted(list_sort4)) # [1, 2, 3, 4, 5]
print(list_sort4) # [1, 2, 5, 4, 3]

list_sort5 = ['ca','sd','feq','rb']
print(sorted(list_sort3, key=lambda i:i[1])) # ['ca', 'rb', 'sd', 'feq']

列表的切片

切片不会改变原列表,会返回切片后的结果。用法:list[start:end:step] ,包含start位置的元素,不包含end位置的元素,step为步长,默认为1

1
2
3
4
5
6
7
8
9
list_origin = [1,2,3,4,5,6,7,8,9,0]
print(list_origin[1:5]) # [2,3,4,5]
print(list_origin[1:5:2]) # [2,4]
print(list_origin[:5]) # [1,2,3,4,5]
print(list_origin[3:]) # [4,5,6,7,8,9,0]
print(list_origin[:]) # [1,2,3,4,5,6,7,8,9,0]
print(list_origin[::]) # [1,2,3,4,5,6,7,8,9,0]
print(list_origin[:4:-1]) # [0,9,8,7,6]
print(list_origin[-1::-1]) # [0,9,8,7,6,5,4,3,2,1]

列表推导

  • 方式一:使用函数range实现
    1
    list_result = [i for i in range(1,10)] # [1, 2, 3, 4, 5, 6, 7, 8, 9] ,同样,range函数中不包含end位置的值
  • 方式二:带有条件的推导
    1
    list_result = [i for i in range(1,10) if i%2==0] # [2,4,6,8]
  • 方式三:嵌套列表推导
    1
    2
    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    list_result = [num for row in matrix for num in row] # [1, 2, 3, 4, 5, 6, 7, 8, 9]
  • 方式四:使用函数的推导
    1
    2
    words = ['hello', 'world', 'python']
    list_result = [word.index('o') for word in words] # [4, 1, 4]
  • 方式五:多重循环的条件推导
    1
    list_result = [(x,y) for x in range(3) for y in range(4)] # [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
  • 方式六:条件表达式使用
    1
    list_result = [0 if x%2==0 else 1 for x in range(1,10)] # [1, 0, 1, 0, 1, 0, 1, 0, 1]
  • 二维列表推导
    1
    2
    3
    4
    5
    6
    7
    8
    matrix = [[i+j for i in range(3)] for j in range(4)]
    for row in matrix:
    print(row)
    # [0, 1, 2]
    # [1, 2, 3]
    # [2, 3, 4]
    # [3, 4, 5]

字典

字典中的值可以取python中的任何对象,但key有如下要求:1、唯一性,不允许同一个key出现两次。2、不可变,key必须不可变,即key必须hashable,因此无法使用列表、字典等作为key,但是可以使用元组、字符串、数字等。

Hashable

python文档中的解释
An object is hashable if it has a hash value which never changes during its lifetime (it needs a hash() method), and can be compared to other objects (it needs an eq() method). Hashable objects which compare equal must have the same hash value.

Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.

All of Python’s immutable built-in objects are hashable; mutable containers (such as lists or dictionaries) are not. Objects which are instances of user-defined classes are hashable by default. They all compare unequal (except with themselves),and their hash value is derived from their id().

可以对数字、字符串等对象进行hash,对列表、集合、字典等进行hash则会报错

1
2
3
4
5
6

print(hash('abc')) # 2038000172280139467
list1 = [1, 2, 3]
print(hash(list1)) # TypeError: unhashable type: 'list'


字典中有如下函数可以使用(可通过print(dir(dict))进行查看):

方法 描述
clear() 移除字典中所有的元素
copy() 复制字典
fromkeys(seq[,value]) 循环从列表seq中取出元素作为key,并使用value作为值,生成一个长度为len(seq)的字典
get(k,default=None) 获取key为k的元素的值,若key=k不存在则返回默认值,但不会抛出异常
items() 将字典转换为可迭代的[(key1,value1),(key2,value2)…]的元组列表
keys 将字典的key转换为可迭代的[key1,key2…]的列表
values 将字典的value转换为可迭代的[value1,value2…]的列表
pop(k[,d]) 返回key=k的元素的value,并将该元素从字典中移除,如果该元素不存在则返回d,若未指定d,则抛出KeyError异常
popitem() 按照LIFO后进先出的顺序返回一个(key,value)的元组,并将该元素从字典中移除,若字典为空,则抛出KeyError异常
setdefault(k,default=None) 插入一个key=k且value=default的元素,并返回value的值,未指定default时默认为None;若k在字典的key中存在,则直接返回key=k的元素的value
update(dict_new) 使用新字典更新原字典,新字典可以是元组列表、关键字表达式或zip(list1,list2)等形式,具体使用方法见下方第11点

字典的创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 字典的创建,如下字典均相等
dict_a = {'one':1,'two':2,'three':3}
dict_b = dict(one=1,two=2,three=3)
dict_c = dict(zip(['one','two','three'],[1,2,3]))
dict_d = dict([('one',1),('two',2),('three',3)])
dict_e = dict({'three':3,'two':2},one=1)

# 和创建字典时类似,可以使用元组列表、关键字或zip等形式更新字典,如下
dict_update.update([(2,'b'),(3,'c')])
print(dict_update) # {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
dict_update.update(one=1,two=2)
print(dict_update) # {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 'one': 1, 'two': 2}
dict_update.update(zip([1,2],['x','y']))
print(dict_update) # {1: 'x', 2: 'y', 3: 'c', 4: 'd', 5: 'e', 'one': 1, 'two': 2}

字典的推导

1
2
3
4
5
6
7
8
9
10
11
tmp_list = [1, 2, 2, 3, 3 ,3]
# 1、基本字典推导:{key:value for var in iterable}
dict_1 = {i:tmp_list.count(i) for i in tmp_list}
print(dict_1) # {1: 1, 2: 2, 3: 3}
# 2、带条件的字典推导:{key:value for var in iterable if_clause}
dict_2 = {i:tmp_list.count(i) for i in tmp_list if i%2 == 1}
print(dict_2) # {1: 1, 3: 3}
# 3、字典键值互换:{v:k for k,v in tmp_dict.items}
tmp_dict = {1:'a',2:'b'}
dict_3 = {v:k for k,v in tmp_dict.items()}
print(dict_3) # {'a': 1, 'b': 2}

元组

元组是python中一种内置的存储有序数据的结构,可存储不同类型的数据,但元组是不可修改的,即不可迭代。元组的主要作用是作为参数传递给函数调用,或者在从函数调用那里获得参数时,保护其内容不被外部接口修改。通常情况下,元组用于保存程序中不可修改的内容。

字典中有如下函数可以使用(可通过print(dir(tuple))进行查看):

方法 描述
count() 返回元素x在元组中的个数
index(x[,start[,end]]) 返回第一个值为x的元素的索引,可选参数start和end用于指定搜索范围

元组的基本操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 创建元组
tuple_temp1 = (2,3,4)
tuple_temp2 = (1,) # 当只有一个元素时,需在元素后加逗号,否则会被识别为该元素的类型
tuple_temp3 = ('a')
print(type(tuple_temp3)) # <class 'str'>
tuple_temp4 = ([2,3])
print(type(tuple_temp4)) # <class 'list'>
# 元组中各方法的使用
# 1、count(x)
tuple_count = (1,2,3,4,3,5)
print(tuple_count.count(3)) # 2
# 2、index(x[,start[,end]])
tuple_index = (1,2,3,4,5,4)
print(tuple_index.index(4)) # 3
print(tuple_index.index(4,4,7)) # 5

具名元组

具名元组namedtuple是collections模块中的一个对象,可以通过help(collections.namedtuple)查看具体用法

1
2
3
4
5
6
>>> help(collections.namedtuple)
Help on function namedtuple in module collections:

namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)
Returns a new subclass of tuple with named fields.

其中各参数的含义如下:

  • typename:元组的名称
  • field_names:元组中各字段
  • rename:默认取值为False,当rename=True时,若字段名无效时(如,重名或使用关键字)则会被自动替换为位置名(如,_1)
  • defaults:具名元组中各字段的默认值,若为iterable对象,则依次对应field_names中字段的缺省值

具体用法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from collections import namedtuple

Rectangle = namedtuple("Rectangle", ['width', 'length'], defaults=[1, 2])
r1 = Rectangle(3, 4)
print(r1.width) # 3
print(r1.ge)

# 从可迭代对象构造元组
r2 = Rectangle._make((5, 4))
print(r2) # Rectangle(width=5, length=4)

# 获取具名元组中各个字段
print(r1._fields) # ('width', 'length')

# 将字典转换为具名元组
dict1 = {'width':1, 'length':2}
nc = Rectangle(**dict1)
print(nc) # Rectangle(width=1, length=2)

# 将具名元组转换为字典
dict2 = r1._asdict()
print(dict2) # {'width': 1, 'length': 2}

# 将具名元组转换为元组
t = tuple(r1)
print(t)

集合