gpt4 book ai didi

python - 如何缩短多个 IF .... IN ... OR 语句?

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:40 24 4
gpt4 key购买 nike

<分区>

如何缩短以下 MWE?

files = ['a.txt', 'b.jpg', 'c.png', 'd.JPG', 'e.JPG']
images = [x for x in files if '.jpg' in x or '.png' in x or '.JPG' in x]
print images

我在想

files = ['a.txt', 'b.jpg', 'c.png', 'd.JPG', 'e.JPG']
images = [x for x in files if ('.jpg' or '.png' or '.JPG') in x]
print images

这是行不通的。

对比这篇文章:Checking file extension , 我也对不关注文件结尾的概括感兴趣。

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