Reputation: 139
I am inside a loop with a variable y having a list in each iteration.
Input:
y= ['0', '1', '2', '3', '4', '5', '6']
Desired output:
create pressure at points 0 1 2 3 4 5 6
I tried using simple access with
print "create pressure at points d% d% d% d% d% d% d%" % ( y[0], y[1], y[2], y[3], y[4], y[5], y[6])
gives error:
KeyError: 0
I want to parse all the value in the list to another software and for that I need to print them. In a specific manner
sensorik.cmd('create pressure at points 0 1 2 3 4 5 6')
if it can save as y[0] etc. those could be parsed as
sensorik.cmd('create pressure at points d% d% d% d% d% d% d% ' % (y[0], y[1], y[2], y[3], y[4], y[5], y[6]))
any suggestions?
Upvotes: 0
Views: 55
Reputation: 191894
Just join
the list
y= ['0', '1', '2', '3', '4', '5', '6']
print 'create pressure at points', ' '.join(y)
# create pressure at points 0 1 2 3 4 5 6
Upvotes: 3
Reputation: 123640
Since the result you want is a string, you don't have to parse the integers.
Instead of d%
, use %s
(note that the percent has to be first):
>>> y= ['0', '1', '2', '3', '4', '5', '6']
>>> print "create pressure at points %s %s %s %s %s %s %s" % ( y[0], y[1], y[2], y[3], y[4], y[5], y[6])
create pressure at points 0 1 2 3 4 5 6
Upvotes: 2