对我来说,解决这个问题(https://leetcode.com/problems/flipping-an-image/)的方法是
class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
return [[bit ^ 1 for bit in reversed(row)] for row in image]
然而,我觉得我不理解列表理解的某些方面,下面给了我一个错误:
class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
return [[bit ^ 1] for bit in reversed(row) for row in image]
为什么会出现这个错误?同样,这也不起作用
class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
return [bit ^ 1 for bit in reversed(row) for row in image]
选中[Python.Docs]: Data Structures - Nested List Comprehensions,它包含一个与您类似的示例
关于理解评估,需要记住的规则(它们无限扩展):
在上述示例中:
相关问题 更多 >
编程相关推荐