已知列表lst中有一组整数,要将所有为奇数的元素置0,所有为偶数的元素保持不变,生成一个新的列表,以下列表推导式正确的是( )
A: [0 for i in lst if i%2!=0]
B: [i for i in lst if i%2==0]
C: [0 for i in lst if i%2!=0 else i]
D: [0 if i%2!=0 else I for i in lst]
A: [0 for i in lst if i%2!=0]
B: [i for i in lst if i%2==0]
C: [0 for i in lst if i%2!=0 else i]
D: [0 if i%2!=0 else I for i in lst]
举一反三
- 已知列表lst=[('cat',5),('dog',8),('bird',6)],分别执行以下表达式,结果与其他三个不一样的是: A: [i[::-1] for i in lst] B: [(i[1],i[0]) for i in lst] C: [(v,k) for k,v in lst] D: [(i[1]+i[0]) for i in lst]
- 已知列表lst=[1,2,3],则分别执行以下代码,结果为[9,4,1]的是: A: for i in lst: i=i**2print(lst) B: for i in range(0,3): lst[i]**=2print(lst) C: for i in range(1,4): lst[3-i]=i**2print(lst) D: for i in range(2,-1,-1): lst[i]**=2print(lst)
- 已知列表lst=[1,2,3],则分别执行以下代码,结果为[1,4,9]的是: A: for i in lst: i=i**2print(lst) B: for i in range(0,3): lst[i]**=2print(lst) C: for i in range(0,3): lst[i]=i**2print(lst) D: for i in range(0,3): lst[i]+=lst[i]print(lst)
- 【多选题】以下能实现求解100以内(含100)的奇数之和偶数之和的程序有: A. n1=0 #奇数之和 n2=0 #偶数之和 i=1 while i<=100: if i%2==0: n2=n2+i else: n1=n1+i i=i+1 print(n1,n2) B. n1=0 #奇数之和 n2=0 #偶数之和 i=0 while i<100: i=i+1 if i%2==0: n2=n2+i else: n1=n1+i print(n1,n2) C. n1=0 #奇数之和 n2=0 #偶数之和 i=100 while i>0: if i%2==0: n2=n2+i else: n1=n1+i i=i-1 print(n1,n2) D. n1=0 #奇数之和 n2=0 #偶数之和 i=101 while i>1: i=i-1 if i%2==0: n2=n2+i else: n1=n1+i print(n1,n2)
- 假设以下程序的功能是:删除列表lst所有值为2的元素,则程序空白处应该填写_______________。lst=[1,2,3,2,2,4,2,5]n=lst._________for i in range(0,n): lst.remove(2)print(lst) A: count('2') B: count(2) C: length() D: index('2')