azmilhafiz
azmilhafiz

Reputation: 55

Get value inside array?

I already got the component value like this:

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

I don't want to be like this:

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

How can I print/get value like this only?

LPE
CLK

Upvotes: 0

Views: 98

Answers (5)

azmilhafiz
azmilhafiz

Reputation: 55

This can be solve by doing split:

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

Upvotes: 0

Nishant Nawarkhede
Nishant Nawarkhede

Reputation: 8400

Its easy to use eval function to do this, The eval function lets a python program run python code within itself.

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

Output:

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

Then use for loop,

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

Output:

LPE
CLK

OR

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

Upvotes: 1

Yogesh D
Yogesh D

Reputation: 1681

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

Upvotes: 1

James Sapam
James Sapam

Reputation: 16950

If Set is set:

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

Upvotes: 1

loki
loki

Reputation: 387

convert from set to list. component_list = list(set(['LPE','CLK'])) and print component_list[0], component_list[1]

Upvotes: 0

Related Questions