获取数组内的值?

时间:2014-02-26 06:05:36

标签: python list python-2.7

我已经得到了这样的组件值:

components = "Set(['LPE', 'CLK'])"

我不想这样:

print components 
Set(['LPE', 'CLK'])

我怎样才能打印/获取这样的价值?

LPE
CLK

5 个答案:

答案 0 :(得分:1)

如果设置了设置:

>>> c = "set(['LPE', 'CLK'])"
>>> eval(c)
set(['LPE', 'CLK'])
>>> for i in eval(c):
...     print i
...
LPE
CLK
>>>

答案 1 :(得分:1)

>>> neglect
['(', ')', "'", ',', '[', ']', 'e', 't', 'S']
>>> result=""
>>> for words in components:
...     if(words in neglect):
...             if(words == ','):
...                     result = result +  " "
...     else:
...             result = result + words
... 
>>> result
'LPE  CLK'

答案 2 :(得分:1)

它易于使用eval函数来执行此操作, eval函数允许python程序在其自身内运行python code

var="set(['LPE', 'CLK'])"
print eval(a)

输出:

set(['LPE', 'CLK'])

然后使用for循环,

for i in set(['LPE', 'CLK']):
     print i

输出:

LPE
CLK

或者

>>> for i in eval("set(['LPE', 'CLK'])"):
...    print i
...
LPE
CLK

答案 3 :(得分:0)

从set转换为list。 component_list = list(set(['LPE','CLK']))  并打印component_list [0],component_list [1]

答案 4 :(得分:0)

这可以通过拆分来解决:

components = "Set(['LPE', 'CLK'])"
x = components.lstrip("Set([").rstrip("])")
for i in x.split(", "):
    z = i.lstrip("'").rstrip("'")
    print z