python基础之逻辑题(1)
python基础之逻辑题(1)
3.1,2,3,4,5能组成多少个互不相同且无重复的三位数?
a=1 b=2 转换 a,b=b,a print(a,b)
v=dict.fromkeys(["k1","k2"],[]) v["k1"].append(666) print(v) v["k1"]=777 print(v) 结果: {"k1":[666],"k2":[666]} {"k1":777,"k2":[666]}
3.1,2,3,4,5 能组成多少个互不相同且无重复的三位数
lst =[] for i in range(1,6): for j in range(1,6): for k in range(1,6): if i!=j and i!=k and k!=j: lst.append((i,j,k)) print(i,j,k) print(len(lst)) 结果: 60个
a=['a,1','b,3,22','c,3,4'] b = ['a,2','b,1','d,2'] 按每个字符串的第一个值,合并a和b和c c = ['a,1,2','b,3,22,1','c,3,4','d,2'] ------------------------------------ c = a.copy() for j in range(len(b)): for i in range(len(c)): if b[j][0] == c[i][0]: c[i] += b[j][1:] break else: c.append(b[j]) print(c)
写: 9,6,3,0 写的时候这样写
实际结果 range(9,-1,-3)
lst = [1,2,4,8,16,32,64,128,256,512,1024,32769,65536,4294967296] ---------------------------------------------------------------------- dic={} for i in lst: dic.setdefault(len(str(i)),[]).append(i) print(dic)
##### 例:转换前 lst = [[1,2,3],[4,5,6],[7,8,9]] ##### 转换后lst = [1,2,3,4,5,6,7,8,9] new_lst=[] for i in lst: new_lst.extend(i) #extend()函数用于在列表末尾一次性追加另一个序列中的多个值 print(new_lst)
a = [1,2,3,[4,5],6] b = a c = copy.copy(a) #浅拷贝把对象复制一遍,但是该对象中引用的其他对象我不复制 d = copy.deepcopy(a) #深拷贝把对象复制一遍,并且该对象中引用的其他对象我也复制 b.append(10) c[3].append(11) d[3].append(12) 请问a,b,c,d的值为 a = [1,2,3,[4,5,11],6,10] b = [1,2,3,[4,5,11],6,10] c = [1,2,3,[4,5,11],6] d = [1,2,3,[4,5,11,12],6]
alist = [2,4,5,6,7] for var in alist: if var % 2 == 0: alist.remove(var) alist的最终结果是? 第一次循环:删除了2留下[4,5,6,7] 第二次循环:跳过4,用5,因为索引是1,留下[4,5,6,7] 第三次循环:索引是2,用6,删除6,留下[4,5,7] 结束 ---------------------------------------------------------------- [4,5,7]
kvps = {'1':'1','2':'2'} theCopy = kvps #相当于是赋值操作 kvps['1'] = 5 #{"1":5,"2":"2"} sum = kvps['1']+theCopy['1'] #5+5 print(sum) #10