繁体   English   中英

如何将两个垂直列表彼此相邻打印

[英]How to print two vertical lists next to each other

我正在尝试编写一个连接4游戏。如果您不知道规则,请查找它,这是一个相对简单的游戏。 我已将每一行都制成一个列表。 但是,它将每一行打印在另一行之下。 我希望它将每一行打印在另一行的旁边。

#Variables that determine how close up or if the slot is full for each column
ZeroTime = 0
OneTime = 0
TwoTime = 0
ThreeTime = 0
FourTime = 0
FiveTime = 0
SixTime = 0

#Makes the List for Each Row
ListZero = ["0",".", ".", ".", ".", ".", ".", "."]
ListOne = ["1",".", ".", ".", ".", ".", ".", "."]
ListTwo = ["2",".", ".", ".", ".", ".", ".", "."]
ListThree = ["3",".", ".", ".", ".", ".", ".", "."]
ListFour = ["4",".", ".", ".", ".", ".", ".", "."]
ListFive = ["5",".", ".", ".", ".", ".", ".", "."]
ListSix = ["6",".", ".", ".", ".", ".", ".", "."]

#Asks The Players For Their Usernames
namo = str(input("Player O what is your name: "))
namx = str(input("Player X what is your name: "))
print (namo + " is using O and " + namx + " is using X")

#Prints Current Board
print ("""
The current board is:

0 1 2 3 4 5 6
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
""")

#Asks the O player for which column they are going to choose
ot = str(input(namo + ", you're O! Enter which column you choose:"))

#Adds the slot
#For Slot 0
if ot == "0":
  ListZero [7 - ZeroTime] = "O"
  print (ListZero [0 + ZeroTime])
  ZeroTime = ZeroTime + 1
if ot == "1":
  ListOne [7 - ZeroTime] = "O"
  print (ListZero [0 + ZeroTime])
  ZeroTime = ZeroTime + 1
if ot == "2":
  ListTwo [7 - ZeroTime] = "O"
  print (ListZero [0 + ZeroTime])
  ZeroTime = ZeroTime + 1
if ot == "3":
  ListThree [7 - ZeroTime] = "O"
  print (ListZero [0 + ZeroTime])
  ZeroTime = ZeroTime + 1
if ot == "4":
  ListFour [7 - ZeroTime] = "O"
  print (ListZero [0 + ZeroTime])
  ZeroTime = ZeroTime + 1

else:
  print ("""We Hit an Error!
  Sorry, we don't have a slot for that. The code only allows these 
slots: 
  0, 1, 2, 3, 4, 5, 6.
 Your turn has been skipped. """) #Added turn has been skipped, I can 
fix that later but we're in the prototype right now

#Prints the Board After That
print ("""
The current board is:
""")

#I was confused on printing lists with the [] and '' so I googled 
online, the base code for this was found online. However, I added 
ListZero, ListOne, etc.
print(*ListZero, sep='\n')
print(*ListOne, sep='\n')
print(*ListTwo, sep='\n')
print(*ListThree, sep='\n')
print(*ListFour, sep='\n')
print(*ListFive, sep='\n')
print(*ListSix, sep='\n')

这是一个简短的片段,介绍了如何垂直相邻地打印两个列表。 也许您可以将此扩展到您的代码。 zip可以接受任意数量的支持迭代的对象。

one = ['a', 'b', 'c']
two = [1, 2, 3]
three = ['q', 'w', 'e']

for x, y, z in zip(one,two, three):
    print(x, y, z)

输出:

a 1 q
b 2 w
c 3 e

这与您的代码相对应。

ListZero = ["0",".", ".", ".", ".", ".", ".", "."]
ListOne = ["1",".", ".", ".", ".", ".", ".", "."]
ListTwo = ["2",".", ".", ".", ".", ".", ".", "."]
ListThree = ["3",".", ".", ".", ".", ".", ".", "."]
ListFour = ["4",".", ".", ".", ".", ".", ".", "."]
ListFive = ["5",".", ".", ".", ".", ".", ".", "."]
ListSix = ["6",".", ".", ".", ".", ".", ".", "."]


for a,b,c,d,e,f,g in zip(ListZero, ListOne, ListTwo, ListThree, ListFour, ListFive, ListSix):
    print(a,b,c,d,e,f)

输出:

0 1 2 3 4 5
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .

希望这会有所帮助。

您可以依次遍历列表元素的位置,而不是顺序打印列表,然后遍历列表:

mylists = [ListZero, ListOne, ListTwo, ListThree, ListFour, ListFive, ListSix]

for i in range(8):
    for alist in mylists:
        print(alist[i], end=' ')
    print()

Vineeth的解决方案更为优雅。

您可以使用提供多维数组的numpy数组。

import numpy 
a = numpy.array([[YOUR LISTS],[YOUR LISTS],...])
print(a.T)

由于连接数为4,因此建议您使用numpy这样您也可以获得对角线:

import numpy as np
a = np.full((7, 6), '.')
print(a)
print('')

上面的代码生成矩阵并将其打印出来。 下面的代码将填充一些单元格并显示如何获取对角线,请参阅https://docs.scipy.org/doc/numpy/reference/generation/numpy.diagonal.html

a[0][0] = 'T'
a[6][0] = 'B'
a[5][0] = 'S'
a[4][0] = 'L'
a[6][1] = 'R'
a[5][1] = 'X'
a[6][5] = 'K'
a[3][3] = 'E'
a[0][5] = 'Z'
a[2][5] = 'M'
a[1][0] = 'N'


for i in range(6):
  print(a.diagonal(i,0,1))

for i in range(5):
  print(a.diagonal(-i-1,0,1))

暂无
暂无

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

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