我有以下列表:
['/my/file.py', 'parallel=2', 'parts=4']
如何在上面提取'2'?现在我有:
if 'parallel=' in argv:
parallel = ?? # item.split('parallel=')[1]
else:
parallel = None
答案 0 :(得分:1)
>>> v
['/my/file.py', 'parallel=2', 'parts=4']
>>> for thing in v:
if 'parallel' in thing:
print thing.split('='), thing.split('=')[-1]
print thing.partition('='), thing.partition('=')[-1]
['parallel', '2'] 2
('parallel', '=', '2') 2
答案 1 :(得分:0)
你可以使用列表理解:
>>> [i.split('=')[1] for i in a if 'parallel' in i]
['2']