2014-10-18 51 views
-1

我有以下列表?現在,我有:獲取項目由startswith

if 'parallel=' in argv: 
    parallel = ?? # item.split('parallel=')[1] 
else: 
    parallel = None 
+1

,準確地爲什麼不註釋掉部分不適合你? – 2014-10-18 19:32:25

+2

廣告如果您解析參數,爲什麼不使用提供的['argparse'庫](https://docs.python.org/2/library/argparse.html)。 – 2014-10-18 19:32:44

+0

@MartijnPieters - 是的,我的壞。我以爲'argv'是'['/my/file.py','parallel = 2','parts = 4']'。抱歉! – 2014-10-18 19:34:05

回答

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 
0

您可以使用列表理解:

>>> [i.split('=')[1] for i in a if 'parallel' in i] 
['2']