Python:根据条件拆分列表?
anims = [f for f in files if f[2].lower() not in IMAGE_TYPES]
从美学和性能角度来看,根据条件将项目列表拆分为多个列表的最佳方法是什么?相当于:
good = [x for x in mylist if x in goodvals]
bad = [x for x in mylist if x not in goodvals]
有什么更优雅的方法吗?
更新:下面是实际的用例,以更好地解释我想要做的事情:
# files looks like: [ ('file1.jpg', 33L, '.jpg'), ('file2.avi', 999L, '.avi'), ... ]
IMAGE_TYPES = ('.jpg','.jpeg','.gif','.bmp','.png')
images = [f for f in files if f[2].lower() in IMAGE_TYPES]
anims = [f for f in files if f[2].lower() not in IMAGE_TYPES]
慕慕森
汪汪一只猫
UYOU
相关分类