将整数从列表解析为命令行

2024-06-16 11:09:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个循环中,变量y在每次迭代中都有一个列表

输入:

y= ['0', '1', '2', '3', '4', '5', '6']

期望输出:

create pressure at points 0 1 2 3 4 5 6

我试着用简单的访问

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])

给出错误:

KeyError: 0

我想将列表中的所有值解析到另一个软件,为此我需要打印它们。以特定的方式

sensorik.cmd('create pressure at points 0 1 2 3 4 5 6')

如果可以另存为y[0]等,则可以将其解析为

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]))

有什么建议吗


Tags: cmd列表软件错误create方式建议at
2条回答

只是join列表

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

因为您想要的结果是一个字符串,所以不必解析整数

使用d%代替%s(注意百分比必须在第一位):

>>> 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

相关问题 更多 >