user19911303
user19911303

Reputation: 449

list of strings differ by suffix

Hi I want to create a list which contains strings differ only by suffix ie. like this

['./name[@val="1"]/Output1', './name[@val="1"]/Output2','./name[@val="1"]/Output3']

I tried to iterate through a for loop and appending the suffix int value like this

dummy = []
for I in range(1,5):
    dummy.append('./Timestamp[@absCycle='"'+i'"']/Output'+i)

Then I realized I can't append int value i to a string, is there any other way to do this?

edit: Also how to do that inside a string? I mean how if I want a list like

 ['./name[@val="1"]/Output', './name[@val="2"]/Output','./name[@val="3"]/Output']

Thank you

Upvotes: 0

Views: 80

Answers (3)

Ankur Ankan
Ankur Ankan

Reputation: 3066

dummy = []
for i in range(1,5):
    dummy.append('./Timestamp[@absCycle="'+ str(i) +'"]/Output'+str(i))

Upvotes: 1

Careal Manic
Careal Manic

Reputation: 202

Use str()

dummy = []
for i in range(1,5):
    dummy.append('./Timestamp[@absCycle='"'+i'"']/Output'+str(i))

Upvotes: 1

DhruvPathak
DhruvPathak

Reputation: 43245

In your code change i to str(i) which typecasts the integer to a string.

You should also try to use single line list comprehension, which is cleaner and more pythonic.

dummy = [ ('./name[@val="1"]/Output' + str(i) ) for i in xrange(0,5)]

And also use formatting instead of concatenations.

[ './name[@val="1"]/Output{0}'.format(i)  for i in xrange(0,5)]

Upvotes: 2

Related Questions