python 查找序列图片_Python程序在由字符组成的数组中查找序列

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,c1、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。

翻译自: https://www.includehelp.com/python/program-to-find-a-series-in-an-array-consisting-of-characters.aspx

python 查找序列图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值