Python-通过应用过滤器从列表创建新列表

时间:2018-10-26 12:39:24

标签: python list

我在下面有一个列表:

list = [NEW - FROM VOLUNTARY SECTOR,NEW - FROM PUBLIC SECTOR,
LEAVER - RESIGNED,LEAVER - RETIRED,NEW - FROM PRIVATE SECTOR,
SECONDEE PRIVATE SECTOR,SECONDEE VOLUNTARY SECTOR]

是否有一种方法可以从上面的列表中创建一个新列表,该列表具有除以单词“ NEW”开头的值以外的所有值。所以我的示例输出如下所示:

sublist = [LEAVER - RESIGNED,LEAVER - RETIRED,
SECONDEE PRIVATE SECTOR,SECONDEE VOLUNTARY SECTOR]

我觉得我应该可以使用

for x in list:
    if list(x)!="NEW*":
        sublist.append(x)

但是不能使它工作

1 个答案:

答案 0 :(得分:1)

尝试:

x = ['NEW - FROM VOLUNTARY SECTOR','NEW - FROM PUBLIC SECTOR','LEAVER - RESIGNED','LEAVER - RETIRED','NEW - FROM PRIVATE SECTOR','SECONDEE PRIVATE SECTOR','SECONDEE VOLUNTARY SECTOR']

y = [i for i in x if not i.startswith("NEW")]

print(y)

输出:

['LEAVER - RESIGNED', 'LEAVER - RETIRED', 'SECONDEE PRIVATE SECTOR', 'SECONDEE VOLUNTARY SECTOR']