这两段python代码有什么区别?
我想检查数组是否包含整数1,2,3作为它的元素?你知道吗
def arrayCheck(nums):
for i in nums:
if(i <= (len(nums)-3)):
if (nums[i] == 1 and nums[i+1] == 2 and nums[i+2] == 3):
return(True)
return(False)
def arrayCheck(nums):
for i in range(0,len(nums)-2):
if (nums[i] == 1 and nums[i+1] == 2 and nums[i+2] == 3):
return(True)
return(False)
第一个对数组给出了错误的答案:
arrayCheck([1,1,2,1,1,1,1,2,3])
但第二个是正确的。你知道吗
第一个块
i
是参数的元素。第二,这只是他们的指数。你知道吗如果您希望第一个迭代索引而不是元素,请使用
顺便说一下,您也可以使用列表切片
相关问题 更多 >
编程相关推荐