如何检查二维数组中的每一行是否至少有 2 个元素?

How to I check if every row in a 2D array has at least 2 elements?

现在每个元素只有一个元素:

dots_list = [ [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21] ]

如何根据每个元素至少有 2 个元素的条件跳出 while 循环,其中第二个元素是一个点。

dots_list = [ [1 . . . .],[2 ..],[3 . .],[4 .],[5 .],(...), [20 . .], [21 . ] ]

点的总数并不重要,因为它可以有无限多个。重要的部分是当列表中的所有数字至少有一个点时如何结束循环。

你可以忽略大部分代码,因为我有那部分在工作,但不确定循环条件。

pd = [ #This is the visual of the pyramid

[                 1                    ], #[0][0]  row and total col# ex [2][1] = 5
[              2,    3                 ], #[1][1]
[            4,   5,    6,             ], #[2][2]
[         7,   8,    9,   10,          ], #[3][3]
[      11,  12,   13,  14,   15,       ], #[4][4]
[    16,  17,   18,  19,  20,    21,   ], #[5][5]

]

x = 0 # represents left and right direction in pd -- adding goes right, subtracting goes left
y = 5 # represents up and down direction in pd -- adding goes down, subtracting goes up

lower_bound = 1 #used as lower bound in dice selection
upper_bound = 4 #used as upper bound in dice selection

move_counter = 0 #used to count the total number of moves made in game

print("Starting position: ",pd[y][x])  # The starting position used for debugging
start_position = pd[y][x] # The starting point of the game [y][x] up/down y, left/right x

#     ----  loop begin ----
#while()


random_roll = random.randint(lower_bound, upper_bound) # Randomly selects a number from 1 to 4 to be used as fair die roll
print("Random Roll: " ,random_roll)
#--------------------------------pd bounds checking-------------------------------------------
if random_roll == 1  and (pd[y][x]) != 2 \
        and (pd[y][x]) != 4 and (pd[y][x]) != 11 and (pd[y][x]) != 16:
   #--------------------------------pd bounds checking-------------------------------------------

    print('upper left')
   # print('x: ', x, 'y:', y)

    if pd[y][x] == 1:
        print('Invalid Direction --  Move Count Increased')
        move_counter += 1
    else:
        new_pos = pd[y-1][x-1]
        y-=1
        x-=1
        #print('x: ', x, 'y:', y)
        print('new pos: ' , new_pos)
        start_position = new_pos
        print('st pos', start_position)

        move_counter += 1

#--------------------------------pd bounds checking-------------------------------------------
elif random_roll == 2  and (pd[y][x]) != 3 \
        and (pd[y][x]) != 6 and (pd[y][x]) != 10 and (pd[y][x]) != 15 and (pd[y][x]) != 21:
    # --------------------------------pd bounds checking-------------------------------------------
    print('upper right')
    #print('x: ', x, 'y:', y)

    if pd[y][x] == 1:
        print('Invalid Direction --  Move Count Increased')
        move_counter += 1
    else:
        new_pos = pd[y-1][x]
        y-=1
        x+=1
       # print('x: ', x, 'y:', y)
        print('new pos: ' , new_pos)
        start_position = new_pos
        print('st pos', start_position)
        move_counter += 1


#--------------------------------pd bounds checking-------------------------------------------
elif random_roll == 3 and (pd[y][x]) != 16 and (pd[y][x]) != 17 \
        and (pd[y][x]) != 18 and (pd[y][x]) != 19 and (pd[y][x]) != 20 and (pd[y][x]) != 21:
# --------------------------------pd bounds checking-------------------------------------------
    print(' down left')

    #print('x: ', x, 'y:', y)
    new_pos = pd[y+1][x]
    x-=1
    y+=1
   # print('x: ', x,  'y:' , y)
    print('new pos: ' , new_pos)
    start_position = new_pos
    print('st pos', start_position)
    move_counter += 1

#--------------------------------pd bounds checking-------------------------------------------
elif random_roll == 4 and (pd[y][x]) != 16 and (pd[y][x]) != 17 \
        and (pd[y][x]) != 18 and (pd[y][x]) != 19 and (pd[y][x]) != 20 and (pd[y][x]) != 21 :
# --------------------------------pd bounds checking-------------------------------------------
    print('down right')

   # print('x: ', x, 'y:', y)
    new_pos = pd[y+1][x+1]
    x+=1
    y+=1

   # print('x: ', x, 'y:', y)
    print('new pos: ' , new_pos)
    start_position = new_pos
    print('st pos', start_position)
    move_counter += 1

else:
    print('Invalid Direction --  Move Count Increased')
    start_position = pd[y][x]
    print('st pos', start_position)
    move_counter += 1

# ---- loop end ----

您可以使用 any 函数并通过检查任何元素的长度是否小于 2 的理解遍历列表的元素。如果任何元素短于 2,则循环将继续,一旦所有元素的长度至少为 2,循环将停止。

例如:

while any(len(x) < 2 for x in pd):
    # do stuff

其中 pd 是您示例中的二维列表。