打印列表的列表,不带括号

发布于 2024-12-19 12:28:56 字数 384 浏览 0 评论 0原文

这里提出了一个有点类似的问题,但答案没有帮助。

我有一个列表列表,特别是诸如..

[[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 技术交流群。

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

发布评论

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

评论(6

陌路黄昏 2024-12-26 12:28:56

以下将执行此操作:

for item in l:
  print item[0], ', '.join(map(str, item[1:]))

其中 l 是您的列表。

对于您的输入,这会打印出来

tables 1, 2
ladders 2, 5
chairs 2

The following will do it:

for item in l:
  print item[0], ', '.join(map(str, item[1:]))

where l is your list.

For your input, this prints out

tables 1, 2
ladders 2, 5
chairs 2
弱骨蛰伏 2024-12-26 12:28:56

如果您不介意输出位于不同的行上:

foo = [["tables", 1, 2], ["ladders", 2, 5], ["chairs", 2]]
for table in foo:
    print "%s %s" %(table[0],", ".join(map(str,table[1:])))

将其全部放在同一行上会稍微困难一些:

import sys
foo = [["tables", 1, 2], ["ladders", 2, 5], ["chairs", 2]]
for table in foo:
    sys.stdout.write("%s %s " %(table[0],", ".join(map(str,table[1:]))))

print

If you don't mind that the output is on separate lines:

foo = [["tables", 1, 2], ["ladders", 2, 5], ["chairs", 2]]
for table in foo:
    print "%s %s" %(table[0],", ".join(map(str,table[1:])))

To get this all on the same line makes it slightly more difficult:

import sys
foo = [["tables", 1, 2], ["ladders", 2, 5], ["chairs", 2]]
for table in foo:
    sys.stdout.write("%s %s " %(table[0],", ".join(map(str,table[1:]))))

print
爱,才寂寞 2024-12-26 12:28:56

在 Python 3.4.x 中,

以下内容将执行此操作:

for item in l:
    print(str(item[0:])[1:-1])

其中 l 是您的列表。

对于您的输入,将打印出:

tables 1, 2
ladders 2, 5
chairs 2

另一种(更干净的)方式将是这样的:

for item in l:
    value_set = str(item[0:])
    print (value_set[1:-1])

产生相同的输出:

tables 1, 2
ladders 2, 5
chairs 2

希望这可以帮助任何可能遇到此问题的人。

In Python 3.4.x

The following will do it:

for item in l:
    print(str(item[0:])[1:-1])

where l is your list.

For your input, this prints out:

tables 1, 2
ladders 2, 5
chairs 2

Another (cleaner) way would be something like this:

for item in l:
    value_set = str(item[0:])
    print (value_set[1:-1])

Yields the same output:

tables 1, 2
ladders 2, 5
chairs 2

Hope this helps anyone that may come across this issue.

我偏爱纯白色 2024-12-26 12:28:56

试试这个:

L = [['tables', 1, 2], ['ladders', 2, 5], ['chairs', 2]]
for el in L:
    print('{0} {1}'.format(el[0],', '.join(str(i) for i in el[1:])))

输出是:

tables 1, 2
ladders 2, 5
chairs 2

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

L = [['tables', 1, 2], ['ladders', 2, 5], ['chairs', 2]]
for el in L:
    print('{0} {1}'.format(el[0],', '.join(str(i) for i in el[1:])))

The output is:

tables 1, 2
ladders 2, 5
chairs 2

{0} {1} is the output string where

{0} is equal to el[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.

毁梦 2024-12-26 12:28:56

这使用纯列表推导式,没有 map()re.sub()for 循环:

print '\n'.join((item[0] + ' ' + ', '.join(str(i) for i in item[1:])) for item in l)

This uses pure list comprehensions without map(), re.sub() or a for loop:

print '\n'.join((item[0] + ' ' + ', '.join(str(i) for i in item[1:])) for item in l)
晨曦慕雪 2024-12-26 12:28:56

在这里(Python3)

尝试:

[print(*oneSet, sep=", ") for oneSet in a]

对于某些a:

a=[["tables", 1, 2], ["ladders", 2, 5], ["chairs", 2]]

print(*list)将列表打开为打印命令中的元素。您可以使用参数 sep=string 来设置分隔符。 :)

Here you go (Python3)

try:

[print(*oneSet, sep=", ") for oneSet in a]

For some a:

a=[["tables", 1, 2], ["ladders", 2, 5], ["chairs", 2]]

print(*list) opens up the list into elements in the print command. You can use a parameter sep=string to set the separator. :)

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