小小白求助
本帖最后由 kaobiel 于 2022-9-13 19:57 编辑用while循环遍历列表找出里面的偶数,然后在把偶数组成新的列表。
exl =
x = 0
while x < exl:
if exl % 2 == 0:
print(f"列表中的偶数:{exl}")
x += 1
---------------------------------------
while x < exl:这里报错TypeError: '<' not supported between instances of 'int' and 'list'
我改成int(exl)也不行这里怎么改,在把偶数组成一个新的列表代码要怎么写{:10_269:}
exl =
new= [] # 空的新列表
for i inexl: # 遍历 exl 列表每一个元素 i
if i % 2 == 0:
print(f"列表中的偶数:{i}")
new.append(i) # 将符合条件的 i 添加到新列表末尾
print(new) # 打印新列表以查看结果
exl =
d =
print('列表中的偶数 : ' , d)
你想用 while 循环,可以这样改代码:
exl =
x = 0
new_exl = []
while x < len(exl):
if exl % 2 == 0:
new_exl.append(exl)
x += 1
print(new_exl)
可以用过滤器过滤出偶数,参考代码:
exl =
result = filter(lambda x : not x % 2, exl)
print(list(result)) 已解决多谢各位{:10_254:}
页:
[1]