慕娘9325324
首先,您可以使用 a 来重写循环:zipdef array_check(nums): for a, b, c in zip(nums, nums[1:], nums[2:]): if a == 1 and b == 2 and c == 3: return True return False然后,使用元组比较:def array_check(nums): for a, b, c in zip(nums, nums[1:], nums[2:]): if (a, b, c) == (1, 2, 3): return True return False然后是内置的:anydef array_check(nums): return any((a, b, c) == (1, 2, 3) for a, b, c in zip(nums, nums[1:], nums[2:]))测试:>>> array_check([1,3,4,1,2,3,5])True>>> array_check([1,3,4,1,3,5])False注意:有关更快的版本,请参阅下面的@juanpa.arrivillaga评论。如果你想模仿功能风格:import operator, functoolsdef array_check(nums): return any(map(functools.partial(operator.eq, (1,2,3)), zip(nums, nums[1:], nums[2:])))但这真的很不合理!