将列表打印到网格中

时间:2014-04-20 00:19:00

标签: python python-3.x

我有一个我想要显示为网格的值列表:

  

List = [" aaaaaaaaa"," bbbbbbbbb"," ccccccccc"]

我希望他们像这样打印出来

a a a a a a a a a

b b b b b b b b b

c c c c c c c c c

6 个答案:

答案 0 :(得分:2)

这两个衬垫足以解决您的问题!

试试这个:

>>> for values in List:
...     print (" ".join([chars for chars in values]), "\n")

输出:

a a a a a a a a a

b b b b b b b b b

c c c c c c c c c

答案 1 :(得分:1)

print("\n".join(" ".join(line) for line in List));

故障:

print(
    "\n".join(           #every line on its own line
        " ".join(line)   #add a space between each character
        for line in List #loop through List
    )
)

修改:(更简单的解决方案)

print("\n".join(map(" ".join, List)))

答案 2 :(得分:0)

一种方式(不是最有效的)将使用2个for循环,一个循环遍历列表中的每个字符串,另一个循环遍历每个字母。并将" "传递给end函数的参数print

for string in List:
    for letter in string:
        print(letter, end = " ")
    print("\n")

<强>输出:

a a a a a a a a a 

b b b b b b b b b 

c c c c c c c c c 

答案 3 :(得分:0)

使用for循环和" ".join()。然后打印换行符的空白字符串(或者您可以使用'\n')。

>>> List = ["aaaaaaaaa","bbbbbbbbb","ccccccccc"]
>>> for k in List:
...     print(" ".join(k))
...     print("")
... 
a a a a a a a a a

b b b b b b b b b

c c c c c c c c c

>>> 

**输出'\n'代替:

a a a a a a a a a


b b b b b b b b b


c c c c c c c c c

答案 4 :(得分:0)

一个班轮:

>>> li=["aaaaaaaaa","bbbbbbbbb","ccccccccc"]
>>> print(''.join(' '+c for s in li for c in s+'\n\n') )
 a a a a a a a a a 

 b b b b b b b b b 

 c c c c c c c c c 

答案 5 :(得分:0)

效率更高:print("\n\n".join((" ".join(list(line))) for line in List))