在Python中的嵌套列表上进行迭代/双重迭代

很抱歉,如果在这里已经回答了类似的问题,但是也许我不知道如何找到它。

我有一些看起来像这样的列表:

A1, A6 = [157, 157, 0], [407, 157, 0]
A2, A7 = [207, 157, 0], [457, 157, 0]

然后我使用另一个列表将它们分组:

A_LIST = [A1, A2, A3, A4, A5, A6, A7, A8, A9, AX]

现在,我想遍历“ A_LIST”,从其中的每个元素中取出第一个和第二个元素。我尝试了几种方法:

def between(x, val):
    if val <= x <= val + 50:
        return True


def check_pressed():
    mouse_x, mouse_y = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

if click != (0, 0, 0):
    i = 0
    for i in A_LIST:
        if between(mouse_x, A_LIST[i][0]) is True and between(mouse_y, A_LIST[i][1]) is True:

This approach brings an error saying unexpected type reffering to "i". I also tried this format: A_LIST[i[0]], but then it said IndexError: list index out of range. Let's say this problem is solved, then how could i "feed" this iteration with nested lists other than A_LIST? Like, taking 10 such sets (B_LIST, C_LIST, etc...), putting them into yet another list over which I would iterate.

提前致谢!

评论
太过于执着
太过于执着

这应该工作:

def between(x, val):
    if val <= x <= val + 50:
        return True
    else :
        return False  # don't forget this one

if click != (0, 0, 0):
    # i = 0  <-- don't need this line
    for i in A_LIST:
        if between(mouse_x, i[0]) and between(mouse_y, i[1]) :
            # something you like
点赞
评论