在列表中查找字符串序列

问题描述 投票:0回答:1

我正在尝试找到一种方法来检测列表中的字符串序列。

即: 对于列表

List1 = ["A", "B", "C", "D", "E"],

如何编写检测序列的程序

"A", "B", "C" and returns / prints "True"? 

谢谢!

List1 = ["A", "B", "C", "D", "E"]
Axiom = "A", "B" (I am aware that this is a tuple, and therefore would not be detected)

for item in List1:
    if List1[0]==Axiom:
          print("True")

预期产出:

True
python list detection
1个回答
0
投票

你可以试试这个:

def find_sequence(l,s):
   return ''.join(s) in ''.join(l)

函数将列表和序列都转换为字符串,并检查序列是否在列表中。 请注意,只有当您要查找的序列未散布在列表中时,它才会按预期工作。

List1  = ["A", "B", "C", "D", "E"]
Axiom  = "A", "B"

find_sequence(List1,Axiom)

输出是

True
© www.soinside.com 2019 - 2024. All rights reserved.