屏幕输出的元组

时间:2013-09-16 01:53:04

标签: python

我有一个关于显示输出数据的问题。这是我的代码:

coordinate = []

z=0
while z <= 10:
    y = 0
    while y < 10:
        x = 0
        while x < 10:
            coordinate.append((x,y,z))
            x += 1
        coordinate.append((x,y,z))
        y += 1
    coordinate.append((x,y,z))
    z += 1
for point in coordinate:
    print(point)

我的输出数据包含我想要删除的逗号和括号。我希望我的输出看起来像这样:

0 0 0
1 0 0
2 0 0

等。没有逗号和括号,只有值。

3 个答案:

答案 0 :(得分:4)

写下最后两行:

for x, y, z in coordinate:
    print(x, y, z)

答案 1 :(得分:0)

除了@flornquake的回答,你可以对那些while

做点什么
import itertools

# If you just want to print this thing, forget about building a list
# and just use the output of itertools.product
coordinate = list(itertools.product(range(0, 10), range(0, 10), range(0, 11)))

for point in coordinate:
    print('{} {} {}'.format(*point))

答案 2 :(得分:0)

假设你使用的是Python 3,你可以这样做:

for point in coordinate:
    print(*point)

“star”符号将元组解包为其各个元素。然后print函数使用默认分隔符显示元素,默认分隔符是单个空格字符。