gpt4 book ai didi

python - 通过小于运行列表以生成更短的列表 python

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:04 25 4
gpt4 key购买 nike

a = [0,1,2,3,4,5,6]
for x in a:
list(x < 4):

预期输出:[0,1,2,3]实际输出:[True, True, True, True, False, False, False]

知道如何得到我想要的吗?

最佳答案

a = [0,1,2,3,4,5,6]
print ([x for x in a if x<4])

输出;

[0, 1, 2, 3]
>>>

评论后编辑:它叫做list comprehension .

关于python - 通过小于运行列表以生成更短的列表 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35590234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com