仅使用元组列表的第一个索引

时间:2018-04-16 02:44:54

标签: python-3.x list for-loop tuples

我目前使用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)

我目前正试图通过循环遍历返回数据的所有变量,但是当我打印它时只出现第一个索引,我不明白问题是什么。任何想法如何解决这个问题?

2 个答案:

答案 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)将为其分配内存,因此您可以在嵌套循环中重用它。