2020-09-04

tech2025-03-26  4

Python学习

练习(接上次) (3)列表[‘A’,‘B’,‘A’,‘B’]去重

lst = {'A','B','A','B'} se = set(lst) lst = list(se) print(lst) ['B', 'A']

(4)求两个集合{6,7,8},{7,8,9}中不重复的元素(差集指的是两个集合交集外的部分)

set1 = {6,7,8} set2 = {7,8,9} set3 = (set1|set2) - (set1&set2) print(set3) {9, 6}

(5)求{‘A’,‘B’,‘C’}中元素在{‘B’,‘C’,‘D’}中出现的次数

set1 = {'A','B','C'} set2 = {'B','C','D'} set3 = set1 & set2 print(len(set3)) 2

1.序列 (1)针对序列的内置函数 lst(sub)把一个可迭代对象转换为列表

a = list() print(a) b = 'I Love LsgoGroup' b = list(b) print(b) c = (1,2,3,5,8) c = list(c) print(c) [] ['I', ' ', 'L', 'o', 'v', 'e', ' ', 'L', 's', 'g', 'o', 'G', 'r', 'o', 'u', 'p'] [1, 2, 3, 5, 8]

(2)tuple(sub)把一个可迭代对象转换为元组

a = tuple() print(a) b = 'I Love LsgoGroup' b = tuple(b) print(b) c = (1,2,3,5,8) c = list(c) print(c) () ['I', ' ', 'L', 'o', 'v', 'e', ' ', 'L', 's', 'g', 'o', 'G', 'r', 'o', 'u', 'p'] [1, 2, 3, 5, 8]

(3)str(obj)把obj对象转换为字符串

a = 123 a = str(a) print(a,type(a)) 123 <class 'str'>

(4)len(s)返回对象(字符,列表,元组等)长度或元素个数

a = 'dsad123' b = [1,'j',1,] c = (1,2,3) print(len(a)) print(len(b)) print(len(c)) 7 3 3

(5)max(sub)返回序列或参数集合中的最大值

a = 'dsad123' b = [1,9,1,] c = (1,2,3) print(max(a)) print(max(b)) print(max(c)) s 9 3

(6)min(sub)返回序列或参数集合中的最小值

a = 'dsad123' b = [1,9,1,] c = (1,2,3) print(min(a)) print(min(b)) print(min(c)) 1 1 1

(7)sum(iterable[,start = 0])返回序列iterable与可选参数start的总和

print(sum([1,3,5,7,9])) print(sum([1,3,5,7,9],10)) 25 35

(8)sorted(iterable,key = None,reverse = False)对所有可迭代对象进行排序操作 (a)iterable–可迭代对象 (b)key–主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自与可迭代对象中,指定可迭代对象中的一个元素来进行排序 (c)reverse–排序规则,reverse = True,降序,reverse = False升序(默认) (d)返回重新排序的列表

x = [-8,99,5,66] print(sorted(x)) print(sorted(x,reverse=True)) t = ({'age':20,'name':'a'},{'age':35,'name':'b'},{'age':25,'name':'c'}) x = sorted(t,key=lambda a: a['age']) print(x) [-8, 5, 66, 99] [99, 66, 5, -8] [{'age': 20, 'name': 'a'}, {'age': 25, 'name': 'c'}, {'age': 35, 'name': 'b'}]

(9)reversed(seq)函数返回一个反转的迭代器

x = [-8,99,5,66] y = reversed(x) print(x) print(y) print(list(y)) [-8, 99, 5, 66] <list_reverseiterator object at 0x000001DCFD9A64A8> [66, 5, 99, -8]

(10)enumerate(sequence,[start = 0])用于将一个可遍历的数据对象(如列表,元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在for循环当中

x = [-8,99,5,66] y = list(enumerate(x)) print(y) for i,j in y: print('{c},{g}'.format(c=i,g=j)) [(0, -8), (1, 99), (2, 5), (3, 66)]

(11)zip(iter1[,iter2[…]]) (a)用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,这样做的好处是节约了不少的内存 (b)我们可以使用list()转换来输出列表 (c)如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用*号操作符,可以将元组解压为列表

a = [1,2,3] b = [4,5,6] c = [4,5,6,7] zipped = zip(a,b) print(zipped) print(list(zipped)) a1,a2 = zip(*zip(a,b)) print(list(a1)) print(list(a2)) <zip object at 0x000001B4483EDCC8> [(1, 4), (2, 5), (3, 6)] [1, 2, 3] [4, 5, 6]

练习 (1)怎么找出序列中的最大,最小值

a = [1,2,3] print(max(a)) print(min(a)) 3 1

(2)sort()和sorted()的区别 sort()只能用在列表当中,sorted()使用的范围更广,sort()对原来的对象进行改变,无返回值,sorted()则是直接创建一个新的对象,并返回这个对象

a = [1,2,3,0] b = (1,2,3,0) print(a.sort()) print(a) print(sorted(a)) print(sorted(b)) None [0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3]

(3)怎么快速求1到100所有整数相加之和

a = sum(range(100)) print(a) 4950

(4)求列表[2,3,4,5]每个元素的立方根

a = [2,3,4,5.1] for i in range(len(a)): a[i] = pow(a[i],1/3) print(a) [1.2599210498948732, 1.4422495703074083, 1.5874010519681994, 1.7213006207263157]

(5)将[‘x’,‘y’,‘z’]和[1,2,3]转成[(‘x’,1),(‘y’,2),(‘z’,3)]的形式

a = ['x','y','z'] b = [1,2,3] c = list(zip(a,b)) print(c) [('x', 1), ('y', 2), ('z', 3)]
最新回复(0)