17.编写一个函数满足以下功能: 计算打印所有参数的和乘以基数(base=3)的结果 16 def sum(*a): 17 sum=0 18 for i in a: 19 sum+=i 20 sum=3*sum 21 return sum 22 s=sum(3,4,5,6) 23 print(s) 24
1. [0,1] [3,2,1,0,1,4]) [0,1,4]
2.l.sort是将列表l本身进行排序,且只能是列表,sorted是将序列进行排序,且生成新的序列。In [1]: t=(1,2,3,56,5,43,) In [2]: t1=sorted(t) In [3]: print(t) (1, 2, 3, 56, 5, 43) In [4]: print(t1) [1, 2, 3, 5, 43, 56] In [5]: l=[1,2,3,56,5,43,] In [6]: l.sort() In [7]: print(l) [1, 2, 3, 5, 43, 56]
3.尝试用两个方式,分别用一行语句实现去除了中的相同项,将结果存放在l2列表中 l1 = ['b','c','d','b','c','a','a'] l2 = []
24 l=[i for i in range(101) if i%2==0] 25 l1=[] 26 for a in l: 27 if a%4!=0 and a%6!=0: 28 l1.append(a)
l2=list(set(l1)) [l2.append(i) for i in l1 if i not in l2]