从python列表中删除特定值

时间:2016-06-09 14:53:19

标签: python-2.7

这是清单 ['默认安全'是',' ACCT INQ',' 3','',&# 39; 00','停止/保持添加',' 5','',' 00',' TOWER INQ' T','',' 00',' ACCT FIELD MNT',' 2&# 39;,'',' 00',' COMB STMT MAINT',' C','', ' 00','货币IM80',' W','',' 00',' MONETARY-IM201' D','',' 00',' OCF INQ'' G&# 39;,'',' 00','访问所有功能','否',' RATE INQ' ,' K''',' 00',' NAME / ADDR CHG',' 4',& #39;',' 00','备忘录',' Z','',' 00& #39;,' FLOOR LIMITS',' 0']

我想删除''和' 00'从列表中

结果应该是这样的

['默认安全'是',' ACCT INQ',' 3','停止/保持添加& #39;,' 5',' TOWER INQ' T',' ACCT FIELD MNT'' 2&#39 ;,' COMB STMT MAINT'' C',' MONETARY IM80'' W',' MONETARY-IM201' ,' D',' OCF INQ' G','访问所有功能''否', '对INQ',' K',' NAME / ADDR CHG',' 4',' MEMO POST', ' Z',' FLOOR LIMITS',' 0']

我试过这个 apa = [aa apa in aa in aa!="''"'或者aa!=" 00"]

获得相同的结果

5 个答案:

答案 0 :(得分:0)

我将如何做到这一点:

for i in list:
    if i = "00":
        del list[i]

您也可以使用:

list.remove('00');

答案 1 :(得分:0)

res = ['DEFAULT SECURITY','YES','ACCT INQ','3','','00','STOP / HOLD ADD','5','','00',' TOWER INQ','T','','00','ACCT FIELD MNT','2','','00','COMB STMT MAINT','C','','00',' MONETARY IM80','W','','00','MONETARY-IM201','D','','00','OCF INQ','G','','00','访问ALL FUNC','NO','RATE INQ','K','','00','NAME / ADDR CHG','4','','00','MEMO POST','Z' ,'','00','FLOOR LIMITS','0']

res = [x代表x中的x,如果x不在('00','')]

print res

['DEFAULT SECURITY','YES','ACCT INQ','3','STOP / HOLD ADD','5','TOWER INQ','T','ACCT FIELD MNT','2 ','COMB STMT MAINT','C','MONATARY IM80','W','MONETARY-IM201','D','OCF INQ','G','ACCESS ALL FUNC','NO', 'RATE INQ','K','NAME / ADDR CHG','4','MEMO POST','Z','FLOOR LIMITS','0']

答案 2 :(得分:0)

lst=['DEFAULT SECURITY', 'YES', 'ACCT INQ', '3', '', '00',
     'STOP/HOLD ADD', '5', '', '00', 'TOWER INQ', 'T', '', '00', 
     'ACCT FIELD MNT', '2', '', '00', 'COMB STMT MAINT', 'C', '',
     '00', 'MONETARY IM80', 'W', '', '00', 'MONETARY-IM201', 'D', 
     '', '00', 'OCF INQ', 'G', '', '00', 'ACCESS ALL FUNC', 'NO',
     'RATE INQ', 'K', '', '00', 'NAME/ADDR CHG', '4', '', '00', 
     'MEMO POST', 'Z', '', '00', 'FLOOR LIMITS', '0']

print [x for x in lst if x != '00' and x != '']

#Output
['DEFAULT SECURITY', 'YES', 'ACCT INQ', '3', 'STOP/HOLD ADD', 
 '5', 'TOWER INQ', 'T', 'ACCT FIELD MNT', '2', 'COMB STMT MAINT',
 'C', 'MONETARY IM80', 'W', 'MONETARY-IM201', 'D', 'OCF INQ', 'G',
 'ACCESS ALL FUNC', 'NO', 'RATE INQ', 'K', 'NAME/ADDR CHG', '4', 
 'MEMO POST', 'Z', 'FLOOR LIMITS', '0']

答案 3 :(得分:0)

使用while循环

list1 =  ['DEFAULT SECURITY', 'YES', 'ACCT INQ', '3', '', '00', 'STOP/HOLD ADD', '5', '', '00', 'TOWER INQ', 'T', '', '00', 'ACCT FIELD MNT', '2', '', '00', 'COMB STMT MAINT', 'C', '', '00', 'MONETARY IM80', 'W', '', '00', 'MONETARY-IM201', 'D', '', '00', 'OCF INQ', 'G', '', '00', 'ACCESS ALL FUNC', 'NO', 'RATE INQ', 'K', '', '00', 'NAME/ADDR CHG', '4', '', '00', 'MEMO POST', 'Z', '', '00', 'FLOOR LIMITS', '0']

while '00' in list1: list1.remove('00')
print(list1)

输出

['DEFAULT SECURITY', 'YES', 'ACCT INQ', '3', '', 'STOP/HOLD ADD', '5', '', 'TOWER INQ', 'T', '', 'ACCT FIELD MNT', '2', '', 'COMB STMT MAINT', 'C', '', 'MONETARY IM80', 'W', '', 'MONETARY-IM201', 'D', '', 'OCF INQ', 'G', '', 'ACCESS ALL FUNC', 'NO', 'RATE INQ', 'K', '', 'NAME/ADDR CHG', '4', '', 'MEMO POST', 'Z', '', 'FLOOR LIMITS', '0']

删除所有'00'字词

答案 4 :(得分:0)

单线:

filter(lambda a: a!='' and a!='00', ['DEFAULT SECURITY', 'YES', 'ACCT INQ', '3', '', '00', 'STOP/HOLD ADD', '5', '', '00', 'TOWER INQ', 'T', '', '00', 'ACCT FIELD MNT', '2', '', '00', 'COMB STMT MAINT', 'C', '', '00', 'MONETARY IM80', 'W', '', '00', 'MONETARY-IM201', 'D', '', '00', 'OCF INQ', 'G', '', '00', 'ACCESS ALL FUNC', 'NO', 'RATE INQ', 'K', '', '00', 'NAME/ADDR CHG', '4', '', '00', 'MEMO POST', 'Z', '', '00', 'FLOOR LIMITS', '0'])

请参阅https://stackoverflow.com/a/1157160/761963