打印列表的列表,不带括号
这里提出了一个有点类似的问题,但答案没有帮助。
我有一个列表列表,特别是诸如..
[[tables, 1, 2], [ladders, 2, 5], [chairs, 2]]
它是一个简单的索引器。
我打算像这样输出它:
tables 1, 2
ladders 2, 5
chairs 2
但我无法得到这样的输出。
然而我可以得到:
tables 1 2
ladders 2 5
chairs 2
但这还不够接近。
有没有一种简单的方法可以完成我的要求?这并不意味着是该计划的困难部分。
A somewhat similar question was asked on here but the answers did not help.
I have a list of lists, specifically something like..
[[tables, 1, 2], [ladders, 2, 5], [chairs, 2]]
It is meant to be a simple indexer.
I am meant to output it like thus:
tables 1, 2
ladders 2, 5
chairs 2
I can't get quite that output though.
I can however get:
tables 1 2
ladders 2 5
chairs 2
But that isn't quite close enough.
Is there a simple way to do what I'm asking? This is not meant to be the hard part of the program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以下将执行此操作:
其中
l
是您的列表。对于您的输入,这会打印出来
The following will do it:
where
l
is your list.For your input, this prints out
如果您不介意输出位于不同的行上:
将其全部放在同一行上会稍微困难一些:
If you don't mind that the output is on separate lines:
To get this all on the same line makes it slightly more difficult:
在 Python 3.4.x 中,
以下内容将执行此操作:
其中 l 是您的列表。
对于您的输入,将打印出:
另一种(更干净的)方式将是这样的:
产生相同的输出:
希望这可以帮助任何可能遇到此问题的人。
In Python 3.4.x
The following will do it:
where l is your list.
For your input, this prints out:
Another (cleaner) way would be something like this:
Yields the same output:
Hope this helps anyone that may come across this issue.
试试这个:
输出是:
{0} {1}
是输出字符串,其中{0}
等于el[0]
其中是'tables'
,'ladders'
, ...{1}
等于', '.join(str( i) for i in el[1:])
和
', '.join(str(i) for i in el[1:])
连接列表中的每个元素:[1,2]
、[2,5 ]
,... 以', '
作为分隔符。str(i) for i in el[1:]
用于在连接之前将每个整数转换为字符串。Try this:
The output is:
{0} {1}
is the output string where{0}
is equal toel[0]
which is'tables'
,'ladders'
, ...{1}
is equal to', '.join(str(i) for i in el[1:])
and
', '.join(str(i) for i in el[1:])
joins each element in the list from these:[1,2]
,[2,5]
,... with', '
as a divider.str(i) for i in el[1:]
is used to convert each integer to string before joining.这使用纯列表推导式,没有
map()
、re.sub()
或for
循环:This uses pure list comprehensions without
map()
,re.sub()
or afor
loop:在这里(Python3)
尝试:
对于某些a:
print(*list)将列表打开为打印命令中的元素。您可以使用参数 sep=string 来设置分隔符。 :)
Here you go (Python3)
try:
For some a:
print(*list) opens up the list into elements in the print command. You can use a parameter sep=string to set the separator. :)