如何将列表的值放入字符串中

发布于 2024-08-17 04:41:34 字数 429 浏览 3 评论 0原文

我正在尝试将列表中的多个值放入字符串中。我的代码如下:

ID = [0, 1, 2]
print 'ID {0}, {1}, and {2}.'.format(ID)

print (r'(ID\s*=\s*)(\S+)').format(ID)

这不起作用。有谁知道我哪里出错了。 第二行的代码打印出列表:

[0, 1, 2]

第一行说:

File "tset.py", line 39, in b
    print 'ID {0}, {1}, and {2}.'.format(ID)
IndexError: tuple index out of range

谢谢

I am trying to place several values from a list into a string. The code I have is below:

ID = [0, 1, 2]
print 'ID {0}, {1}, and {2}.'.format(ID)

or

print (r'(ID\s*=\s*)(\S+)').format(ID)

This does not work. Does anyone know where I'm going wrong.
The code in the second line prints out the list:

[0, 1, 2]

the first line says:

File "tset.py", line 39, in b
    print 'ID {0}, {1}, and {2}.'.format(ID)
IndexError: tuple index out of range

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青丝拂面 2024-08-24 04:41:34

您必须解压该列表。

ID = [0, 1, 2]
print 'ID {0}, {1}, and {2}.'.format(*ID)

请参阅文档:解压参数列表

You have to unpack the list.

ID = [0, 1, 2]
print 'ID {0}, {1}, and {2}.'.format(*ID)

See the docs: Unpacking argument lists.

所有深爱都是秘密 2024-08-24 04:41:34
>>> 'ID {0}, {1}, and {2}.'.format(*ID)
'ID 0, 1, and 2.'

你需要打开你的清单。

你的第二个代码没有多大意义。

>>> 'ID {0}, {1}, and {2}.'.format(*ID)
'ID 0, 1, and 2.'

You need to unpack your list.

Your second code doesn't make much sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文