请问python如何在list的元素中包含某些字符时删掉该元素,得到新list

比如,list中有包含‘.rar'或者’.txt'或者‘原始文件’等字符的元素,但是不完全等于,可能该元素是‘abcd.rar',如何使得在元素中只要出现’.rar'等字符时,就删掉该元素呢?

翻翻过去那场雪
浏览 4794回答 2
2回答

桃花长相依

>>> olist = ['hello', 'abcd.rar', '原始文件', 'world.txt', 'yes']>>> nlist=[]>>> for r in olist: if '.rar' not in r and '.txt' not in r and  '原始文件' not in r: nlist.append(r) >>> nlist['hello', 'yes']可以使用not in

潇潇雨雨

import rep = re.compile('.rar|.txt|原始文件')olist = ['hello', 'abcd.rar', '原始文件', 'world.txt', 'yes']nlist = [x for x in olist if not p.findall(x)]
打开App,查看更多内容
随时随地看视频慕课网APP