面试题汇总
1.用一行代码令a,b的值互换
a,b = 1,2 print(a,b) a = 1 b = 2 a,b = b,a #面试题 print(a,b) a,b = [1,2],[2,3] print(a,b) a,b = (1,2) print(a,b)
2、去重
#去重 li = [1,2,33,33,2,1,4,5,6,6] set1 = set(li) # print(set1) li = list(set1) print(li) s1 = {1,2,3} print(s1,type(s1))
3.生成器函数面试题
面试题一
# 面试题1
def demo():
for i in range(4):
yield i
g = demo()
g1 = (i for i in g)
g2 = (i for i in g1)
#print(list(g))
#print(list(g1))
print(list(g2))
面试题2
# 面试题2
def add(n,i):
return n+i
def test():
for i in range(4):
yield i
g = test() #得到生成器函数,但是并没有执行函数内部的代码
for n in [1, 10]:
g=(add(n, i) for i in g)
print(list(g))
# 执行过程:
# n = 1
# g = (add(n, i) for i in g)
# n = 10
# g = (add(n, i) for i in g)
# n = 5
# g = (add(n, i) for i in g)
# g = (add(n, i) for i in (add(n, i) for i in (add(n, i) for i in test())))
# g = (add(n, i) for i in (add(n, i) for i in (add(n, i) for i in [0,1,2,3]))) # n = 5
# g = (add(n, i) for i in (add(n, i) for i in [5,6,7,8])) # n = 5
# g = (add(n, i) for i in [10,11,12,13]) # n = 5
# g = [15,16,17,18]