面试题
9、[[1,2],[3,4],[5,6]]一行代码展开该列表,得出[1,2,3,4,5,6]
12、python中交换两个数值
5. 请用自己的算法, 按升序合并如下两个list, 并去除重复的元素:
list1 = [2, 3, 8, 4, 9, 5, 6]
list2 = [5, 6, 10, 17, 11, 2]
解答:先转换成集合自动去重,再转换成列表。
6. 请写出打印结果:
x = [0, 1]
i = 0
i, x[i] = 1, 2
print(x)
打印结果: [0, 2], python可以使用连续赋值, 从左至右.
11.
g = lambda x, y=2, z : x + y**z
g(1, z=10) = ?
打印结果: 异常, 形参表末尾才可以有默认参数, z需要提供默认参数.
11.以下的代码的输出将是什么? 说出你的答案并解释。
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print (Parent.x, Child1.x, Child2.x)
Child1.x = 2
print (Parent.x, Child1.x, Child2.x)
Parent.x = 3
print (Parent.x, Child1.x, Child2.x)