嗨,我有一个名为' d'和一个清单' to_be_removed'其中列表中的值如下:
d:
{'data': [' VL0002511CA 000000000682414100000000000110000 ',
' VL0002511ZZ 000000000682414100000000000110000 ',
' VL0002512 PRE1985000000000682414100000000000110000 ',
' VL0002521CA 000000001888990100000000000310000 ',
' VL0002521ZZ 000000001888990100000000000310000 ',
' VL0002522 PRE1985000000001888990100000000000310000 ',
' VL0002531CA 000000001223831100000000000210000 ',
' VL0002531ZZ 000000001223831100000000000210000 ',
' VL0002532 PRE1985000000001223831100000000000210000 ',
' VL0007871CA 000000001463787100000000000210000 ',
' VL0007871ZZ 000000001463787100000000000210000 ']}
to_be_removed:
Out[14]:
['ZZ', 'PRE']
我需要删除那些包含字符串' ZZ'和' PRE'从列表中d。所以我的输出列表应该只是值:
final_list:
{'data': [' VL0002511CA 000000000682414100000000000110000 ',
' VL0002521CA 000000001888990100000000000310000 ',
' VL0002531CA 000000001223831100000000000210000 ',
' VL0007871CA 000000001463787100000000000210000 ']}
我怎么能在python中做到这一点?
答案 0 :(得分:2)
您可以按如下方式使用列表推导:
d = {'data': [' VL0002511CA 000000000682414100000000000110000 ',
' VL0002511ZZ 000000000682414100000000000110000 ',
' VL0002512 PRE1985000000000682414100000000000110000 ',
' VL0002521CA 000000001888990100000000000310000 ',
' VL0002521ZZ 000000001888990100000000000310000 ',
' VL0002522 PRE1985000000001888990100000000000310000 ',
' VL0002531CA 000000001223831100000000000210000 ',
' VL0002531ZZ 000000001223831100000000000210000 ',
' VL0002532 PRE1985000000001223831100000000000210000 ',
' VL0007871CA 000000001463787100000000000210000 ',
' VL0007871ZZ 000000001463787100000000000210000 ']}
to_be_removed = ['PRE', 'ZZ']
updated = {'data': [item for item in d['data'] if not any(subst in item for subst in to_be_removed)] }
print(updated)
<强>输出强>
{'data': [' VL0002511CA 000000000682414100000000000110000 ',
' VL0002521CA 000000001888990100000000000310000 ',
' VL0002531CA 000000001223831100000000000210000 ',
' VL0007871CA 000000001463787100000000000210000 ']}
要允许使用多个键的词典,请使用词典理解:
updated = {k: [item for item in v if not any(subst in item for subst in to_be_removed)] for k, v in d.items()}
答案 1 :(得分:0)
您可以使用all功能和列表生成/理解。
d={'data': [' VL0002511CA 000000000682414100000000000110000 ',
' VL0002511ZZ 000000000682414100000000000110000 ',
' VL0002512 PRE1985000000000682414100000000000110000 ',
' VL0002521CA 000000001888990100000000000310000 ',
' VL0002521ZZ 000000001888990100000000000310000 ',
' VL0002522 PRE1985000000001888990100000000000310000 ',
' VL0002531CA 000000001223831100000000000210000 ',
' VL0002531ZZ 000000001223831100000000000210000 ',
' VL0002532 PRE1985000000001223831100000000000210000 ',
' VL0007871CA 000000001463787100000000000210000 ',
' VL0007871ZZ 000000001463787100000000000210000 ']}
to_be_removed=['ZZ', 'PRE']
lst = []
for j in d['data']:
if all(not i in j for i in to_be_removed):
lst.append(j)
final_data = {'data': lst}
print final_data
或只是一个班次 -
print {'data': [j for j in d['data'] if all(not i in j for i in to_be_removed)]}
输出 -
{'data': [' VL0002511CA 000000000682414100000000000110000 ', ' VL0002521CA 000000001888990100000000000310000 ', ' VL0002531CA 000000001223831100000000000210000 ', ' VL0007871CA 000000001463787100000000000210000 ']}
答案 2 :(得分:0)
您可以使用re.sub
,list comprehension
和dict comprehension
执行此操作:
In [182]: {'data' : [l2 for l2 in [re.sub('.*[(ZZ)|(PRE)]+.*', '', l1) for l1 in d['data']] if l2]}
Out[182]:
{'data': [' VL0002511CA 000000000682414100000000000110000 ',
' VL0002521CA 000000001888990100000000000310000 ',
' VL0002531CA 000000001223831100000000000210000 ',
' VL0007871CA 000000001463787100000000000210000 ']}