Python Disambiguation
List Comprehension
Looking at the below code, write down the final values of A0, A1, …An.
A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5))) A1 = range(10) A2 = sorted([i for i in A1 if i in A0]) A3 = sorted([A0[s] for s in A0]) A4 = [i for i in A1 if i in A3] A5 = {i:i*i for i in A1} A6 = [[i,i*i] for i in A1]
Link: problem source.
The trap of lists = [[]] * 5:
>>> lists = [[]] * 5 >>> lists [[], [], [], [], []] >>> lists[0].append(1) >>> lists [[1], [1], [1], [1], [1]] >>> id(lists[2]) 139762852067080 >>> id(lists[3]) 139762852067080
TODO: what’s the root cause?
Print Function
Print with specific number of digits:
print("{:2d}".format(1)) >>> _1 print("{:02d}".format(1)) >>> 01 print("{:02d}".format(1100)) >>> 1100 print("{:.2f}".format(1.5)) >>> 1.50
Relative Import Path
TODO: why and how to solve it?
Link: stackoverflow explanation.