2014-08-29 62 views
0

我需要使用列表值作爲變量。它怎麼能做到。如何使用列表數據作爲變量

comp_list = [ "list1", " list2", "list3"] 
for comp in comp_list: 
    print (comp) 
    cmd = 'ps -aef | grep $comp'<<<< 
    print (cmd) 
    status, command = getstatusoutput(cmd) 

<<<引導到$complist1被替換,然後list2,它應該繼續下去。

+0

你的意思是你需要用'comp'的值替換'$ comp'? – justanothercoder 2014-08-29 05:29:38

+0

''ps -aef | grep'+ str(comp)' – 2014-08-29 05:36:50

回答

1
cmd = 'ps -aef | grep $comp'<<<< 

這可能就像這樣:

cmd = 'ps -aef | grep %s' % comp 
1

您既可以使用str.format

cmd = 'ps -aef | grep {}'.format(comp) 

或者只是連接字符串:

cmd = 'ps -aef | grep ' + comp