红糖糍粑
您还必须考虑负指数。def in_first_col(put, list2D): row_idx, col_idx = put # return False if we can't get the row try: row = list2D[row_idx] except IndexError: return False # return False if row is empty # return False if col_idx does not refer to first element of row return row and (col_idx == 0 or col_idx == -len(row))演示:>>> rix = [[1,2,3],[4,5,6],[7,8,9]]>>> in_first_col([1,1], rix)>>> False>>> in_first_col([2,0], rix)>>> True>>> in_first_col([5,0], rix)>>> False>>> in_first_col([-2,-3], rix)>>> True