簡體   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