rows = range(1,4) cols = range(1,3) cells = [(row, col) for row in rows for col in cols] print(cells) cells = [(row, col) for row in rows if row !=2 for col in cols if col !=1] print(cells)
输出:
[(1, 1), (1, 2), (2, 1), (2, 2), (3, 1), (3, 2)] [(1, 2), (3, 2)]
参考资料:《Python语言及其应用》卢布诺维克 著



