Python-面试题-集合(set)和字典(dict)

"""
4.1 输出1-100除3余1 的数,结果为tuple

输出1-100除3余1 的数,结果为tuple
"""
a = [j for j in range(1,101) if j % 3==1]
print(set(a))


""" 4.2 把2个元祖转字典 将('a', 'b', 'c', 'd', 'e') 和 (1,2, 3, 4, 5)两个tuple转成 (1, 2, 3, 4, 5)为key, ('a', 'b', 'c', 'd', 'e') 为value的字典 """ a = ('a', 'b', 'c', 'd', 'e') b = (1,2, 3, 4, 5) print(dict(zip(b,a))) #{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}


""" 4.3 把字典的value值转成str 将字典里的值是数值型的转换为字符串,如a = {'aa': 11, 'bb': 222} 得到{'aa': '11', 'bb': '222'} """ #方法一: a = {'aa': 11, 'bb': 222} for k,v in a.items(): a[k]=str(v) print(a) #{'aa': '11', 'bb': '222'} #方法二: a = {'aa': 11, 'bb': 222} b = {} for i ,j in a.items(): print(i,j) b[i]=str(j) print(b) #{'aa': '11', 'bb': '222'}


""" 4.4 (1)和(1,)区别,[1]和[1,] a = [1,2,3] 和 b = [(1),(2),(3) ] 以及 c = [(1,),(2,),(3,) ] 的区别? """ print(isinstance((1,),tuple)) #True print(isinstance((1),int)) #True a = [1,2,3] b = [(1),(2),(3) ] c = [(1,),(2,),(3,) ] print(isinstance(a,list)) #True print(isinstance(b,list)) #True print(isinstance(c,list)) #True


""" 4.5 map函数将[1,2,3,4]处理成[1,0,1,0] map函数,有个列表a = [1, 2, 3, 4] 计算列表中每个数除以2 取出余数 得到 [1,0,1,0] """ a = [1, 2, 3, 4] #方法一: def my_func(x): return x % 2 print(list(map(my_func,[x for x in a]))) #[1, 0, 1, 0] #方法二 print(list(map(lambda x:x%2,[x for x in a]))) #[1, 0, 1, 0]


""" 4.6 map函数将列表[1,2,3,4,5]转变成[1,4,9,16,25] map函数将列表 [1,2,3,4,5] 使用python方法转变成 [1,4,9,16,25] """ a = [1,2,3,4,5] print(list(map(lambda x : x**2,[x for x in a])))


""" 4.7 map函数a=[1,3,5],b=[2,4,6]相乘得到[2,12,30] map函数对列表a=[1,3,5],b=[2,4,6]相乘得到[2,12,30] """ a=[1,3,5] b=[2,4,6] print(list(map(lambda x, y: x*y, [x for x in a], [y for y in b])))


""" 4.8 reduce函数计算1-100的和 reduce函数计算1-100的和 """ #reduce() 函数会对参数序列中元素进行累积,即先对集合中的第1,2个元素进行操作,得到的结果再与第3个数据用function函数运算 # Python3.x reduce() 已经被移到 functools 模块里 from functools import reduce #方法一: def my_func1(x,y): return x + y print(reduce(my_func1,[x for x in range(1,101)])) #方法二: print(reduce(lambda x, y: x + y,[x for x in range(1,101)]))


""" 4.9 reduce函数计算10! reduce函数计算1!+2!+3!+。。。+10! """ from functools import reduce print(reduce(lambda x, y: x * y,range(1,11))) b = reduce(lambda a,b: a+b, [reduce(lambda x, y: x*y, range(1, i+1)) for i in range(1, 11)]) print(b)


""" 4.10 两个字典合并a={"A":1,"B":2},b={"C":3,"D":4} 两个字典合并a={"A":1,"B":2},b={"C":3,"D":4} """ a={"A":1,"B":2} b={"C":3,"D":4} print(a.update(b)) print(a)


""" 4.11 {'a':1,'b':2,'c':1} 得到 {1:['a','c'],2:['b']} m1={'a':1,'b':2,'c':1} """ m1={'a': 1,'b': 2,'c': 1} d = {} b = [(i[1],i[0]) for i in m1.items()] print(b) for i in b: if i[0] not in d.keys(): d[i[0]]=[i[1]] else: d[i[0]].append(i[1]) print(d)

""" 4.12 字典按key排序d={"name":"zs","age":18,"} d={"name":"zs","age":18,"city":"深圳","tel":"1362626627"} 字典根据键从小到大排序 """ from collections import OrderedDict d={"name":"zs","age":18,"city":"深圳","tel":"1362626627"} a = OrderedDict(sorted(d.items(), key=lambda x: x[0])) print(dict(a))


""" 4.13 集合(交集、差集、并集) a = [2, 3, 8, 4, 9, 5, 6] b = [2, 5, 6, 10, 17, 11] 1.找出a和b中都包含了的元素 2.a或b中包含的所有元素 3.a中包含而集合b中不包含的元素 """ a = [2, 3, 8, 4, 9, 5, 6] b = [2, 5, 6, 10, 17, 11] # c = set(a).intersection(set(b)) #交集 # print(c) # d = set(a).difference(set(b)) #差集 # print(d) #a和b中都包含了的元素 c = set(a) & set(b) #并集 print(c) #{2, 5, 6} #a或b中包含的所有元素 f = set(a) | set(b) print(f) #{2, 3, 4, 5, 6, 8, 9, 10, 11, 17} #a中包含而集合b中不包含的元素 e = set(a) - set(b) print(e) #{8, 9, 3, 4} #不同时包含于a 和b 的元素 g = set(a) ^ set(b) print(g) #{3, 4, 8, 9, 10, 11, 17}
三十六般武艺,七十二般变化,修练出个人品牌并发出光芒
原文地址:https://www.cnblogs.com/deeptester-vv/p/14976772.html