我已经得到了这样的组件值:
components = "Set(['LPE', 'CLK'])"
我不想这样:
print components
Set(['LPE', 'CLK'])
我怎样才能打印/获取这样的价值?
LPE
CLK
答案 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