【问题标题】:Printing a list of lists, without brackets打印列表列表,不带括号
【发布时间】:2012-01-12 08:18:54
【问题描述】:

在此处提出了一个类似的问题,但答案没有帮助。

我有一个列表列表,特别是类似..

[[tables, 1, 2], [ladders, 2, 5], [chairs, 2]]

它是一个简单的索引器。

我打算这样输出它:

tables 1, 2
ladders 2, 5
chairs 2

但我无法得到完全的输出。

但是我可以得到:

tables 1 2
ladders 2 5
chairs 2

但这还不够接近。

有没有一种简单的方法来完成我的要求?这并不是该计划的难点。

【问题讨论】:

  • 向我们展示您的尝试和期望
  • 您应该显示您尝试过的代码

标签: python list printing


【解决方案1】:

下面会做:

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

l 是您的列表。

为了您的输入,这会打印出来

tables 1, 2
ladders 2, 5
chairs 2

【讨论】:

  • @LarryLustig:据我所见,输出是 完全 作为 OP 要求的,包括空格和逗号。
  • 我刚试过这个:[['wind', 1, 2], ['blow', 1], ['form', 1], ['south', 1], [ 'strong', 2]] 我得到了以下输出:wi, n, d 后跟一个关于为什么 int 不可下标的回溯错误
  • @user1079404:我刚刚测试了您列表中的代码,它可以工作。确保将其应用于整个列表,而不是每个元素。
  • 对不起,我明白我做错了什么。代码工作正常,但是,我不明白“加入”的工作方式,所以它总是将字符串(在本例中为',')添加到迭代中每个字符串的末尾?
  • @user1079404:它在连续元素之间插入, 。见docs.python.org/library/stdtypes.html#str.join
【解决方案2】:

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

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

【讨论】:

  • 另外,可以在 Python 3 的 print 函数中使用 end='' 作为参数,或者在 Python 2 中在开头添加 from __future__ import print_function 语句。
  • 来自__future__ import print_function 似乎在 2.6 中可用,但在 2.4 中不可用。
【解决方案3】:

在 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

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

【讨论】:

    【解决方案4】:

    试试这个:

    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:] 用于在加入之前将每个整数转换为字符串。

    【讨论】:

    • 这很有道理,感谢您的解释,但它也产生了与我对第一个答案的响应相同的奇怪输出。它将打印如下内容: [['wind', 1, 2], ['blow', 1], ['form', 1], ['south', 1], ['strong', 2]] I得到输出:wi, n, d 后跟一个关于为什么 int 不可下标的回溯错误
    • @user1079404 我不明白出了什么问题。该代码对我有用。
    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

      给你(Python3)

      尝试:

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

      对于一些:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-12
        • 1970-01-01
        • 2012-06-26
        • 1970-01-01
        • 1970-01-01
        • 2013-02-20
        相关资源
        最近更新 更多