我在下面有一个列表:
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)
但是不能使它工作
答案 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']