我目前使用Zip函数返回元组列表:
返回的数据是:List<String> securityCodesPreSend = new List<String> { "1", "2", "3", "4" };
List<String> securityCodesSent = securityCodesPreSend
.Take( 15 )
.ToList();
我循环访问数据以使用它,但目前只打印第一个索引。
[(13, 3), (12, 3), (11, 3), (10, 3), (9, 3), (8, 3), (6, 3), (5, 3), (4, 3)]
这是我从以下地址获取信息的代码:
CheckPath = self.CheckQueenPathDown(QueenRowColumn,TheComparisonQueen) #This is where the list of tuples is being used
print(CheckPath) # this shows all the correct data when i print it.
for TheQueenMoves in QueenMoves:
for a,b in list(self.pieces.items()):
for CheckThePath in CheckPath:
if TheComparisonQueen == TheQueenMoves and TheComparisonQueen[0] >= 0 and TheComparisonQueen[1] <= 7 and \
TheComparisonQueen[1] >= 0 and TheComparisonQueen[0] <= 7 and CheckThePath != b: # this is the line im trying to use it in.
self.placepiece(piece, row = MoveRow, column = MoveColumn)
print(CheckThePath)
我目前正试图通过循环遍历返回数据的所有变量,但是当我打印它时只出现第一个索引,我不明白问题是什么。任何想法如何解决这个问题?
答案 0 :(得分:1)
zip
没有在Python 3上创建列表。如果您需要列表,请在结果上调用list
。
在Python 3上,zip
返回一个迭代器,迭代一次后就会耗尽。如果你试图重复使用它,你就不会得到任何元素。
答案 1 :(得分:0)
<强>尝试:强>
def CheckQueenPathDown(self, QueenRowColumn, TheComparisonQueen):
row = []
column = []
CurrentLocation = QueenRowColumn
#MoveLocation = TheComparisonQueen
a = QueenRowColumn[0]
b = QueenRowColumn[1]
for i in range (-10,0):
row.append(CurrentLocation[1] - i)
column.append(a)
Down = zip(row,column)
#Down.remove(TheComparisonQueen)
return list(Down)
由于return list(ZIP_OBJ)
将为其分配内存,因此您可以在嵌套循环中重用它。