python 查找序列图片
In this article, we would learn weather our array contains a mentioned series or not like a, b, c or 1, 2, 3 etc.
在本文中,我们将学习数组包含提到的序列或不像a,b,c或1、2、3等的天气。
Beginning with a simple question,
从一个简单的问题开始
Question:
题:
We are given with an array of char, return True if the sequence of char a, b, c appears in the array somewhere.
给定一个char数组,如果char a,b,c的序列出现在数组中某处,则返回True 。
Example:
例:
Array_abc(['a', 'x', 'a', 'b', 'c']) = True
Array_abc(['f', 'x', 'a', 'i', 'c', 't']) = True
Array_abc(['k', 'x', 'a', 'e', 'c']) = True
Code:
码:
def Array_abc(char):
for i in range(len(char) - 2):
if char[i] == 'a' and char[i + 1] == 'b' and char[i + 2] == 'c':
return True
return False
print (Array_abc(['a', 'x', 'a', 'b', 'c']))
Output
输出量
True
Explanation:
说明:
Here one can easily be confused in the second line as we have taken range(len(char)-2), in almost all questions we use -1, but here we have used -2. This can be explained because with length-2, we can use i+1 and i+2 in the loop. As we have to find a sequence for three numbers.
由于我们采用了range(len(char)-2) ,因此在第二行中很容易混淆,在几乎所有问题中我们都使用-1 ,但是在这里我们使用了-2 。 这可以解释为因为使用length-2 ,我们可以在循环中使用i + 1和i + 2 。 由于我们必须找到三个数字的序列。
Further, the code is very simple as we have to write an if statement to check the three conditions and if all the three conditions satisfied return True.
此外,代码非常简单,因为我们必须编写一个if语句来检查这三个条件,并且如果所有三个条件都满足,则返回True。
python 查找序列图片