-1

出力データの表示について質問を受けました。これが私のコードです:

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

など。コンマと括弧はなく、値だけです。

4

3 に答える 3

4

最後の 2 行を次のように記述します。

for x, y, z in coordinate:
    print(x, y, z)
于 2013-09-16T01:54:34.887 に答える
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))
于 2013-09-16T02:14:53.640 に答える
0

Python 3 を使用していると仮定すると、次のようにすることができます。

for point in coordinate:
    print(*point)

「スター」表記は、タプルを個々の要素にアンパックします。次に、このprint関数は、単一のスペース文字であるデフォルトのセパレーターを使用して要素を表示します。

于 2013-09-16T02:19:32.933 に答える