简体   繁体   English

如何在左侧和右侧添加顺序,如 1 到 8

[英]How to add on the left and on the right order like 1 to 8

Hi I have small problem I dont know how to add oder like from 1 to number 8 on the right and on the left of this program.您好,我有一个小问题,我不知道如何在该程序的右侧和左侧添加从 1 到数字 8 的顺序。 Here is the list but How to add numbers on the left and on the righ.这是列表,但如何在左侧和右侧添加数字。 I did this with letters up and down我用上下字母做了这个

Here is my code这是我的代码

sachy = [[0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 2, 0, 2, 0, 2], [2, 0, 2, 0, 2, 0, 2, 0], [0, 2, 0, 2, 0, 2, 0, 2]]

poradi = ["a", "b", "c", "d", "e", "f", "g", "h"]
poradi_2 = [1, 2, 3, 4, 5, 6, 7, 8]

for prvek in poradi:
    print(prvek, end=" ")
print()
print()

for seznam in sachy:
    for prvek in seznam:
        print(prvek, end=" ")
    print(end="\n", )

print()

for prvek in poradi:
    print(prvek, end=" ")

I try to write another list of order from 1 to 8 into the seznam but will always multiple by 8 becaouse of sachy that are 8x8.我尝试将另一个从 1 到 8 的顺序列表写入 seznam,但由于 8x8 的 sachy,它总是乘以 8。

You need to pair the row indices with the row itself, also use " ".join() for shorted code您需要将行索引与行本身配对,还需要使用" ".join()来缩短代码

print(" ", " ".join(poradi), "\n")

for idx, seznam in zip(poradi_2, sachy):
    print(idx, " ".join(map(str, seznam)), idx)

print("\n ", " ".join(poradi), "\n\n")
  a b c d e f g h

1 0 1 0 1 0 1 0 1 1
2 1 0 1 0 1 0 1 0 2
3 0 1 0 1 0 1 0 1 3
4 0 0 0 0 0 0 0 0 4
5 0 0 0 0 0 0 0 0 5
6 0 2 0 2 0 2 0 2 6
7 2 0 2 0 2 0 2 0 7
8 0 2 0 2 0 2 0 2 8

  a b c d e f g h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM