Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果两个字符串以特定顺序出现,则从列表中获取元素_Python_Python 3.x_String_List_Sublist - Fatal编程技术网

Python 如果两个字符串以特定顺序出现,则从列表中获取元素

Python 如果两个字符串以特定顺序出现,则从列表中获取元素,python,python-3.x,string,list,sublist,Python,Python 3.x,String,List,Sublist,假设我有以下列表 lst=[['AAAB'],['ACAC'],['CABA'],['AACC'],['BACB'],['BCAA'],['CBAA'],['ABBC']] 我需要提取A出现然后C出现的列表,而不考虑它们之间的其他字母 因此,结果一定是这样的 res=[['ACAC'],['AACC'],['BACB'],['ABBC']] 我可以在另一个for循环中实现一个for循环,该循环进入每个列表,然后遍历每个字符串,并查看是否出现a和C,但我对如何保持幻影的顺序有点困惑,即a出现在

假设我有以下列表

lst=[['AAAB'],['ACAC'],['CABA'],['AACC'],['BACB'],['BCAA'],['CBAA'],['ABBC']]
我需要提取A出现然后C出现的列表,而不考虑它们之间的其他字母

因此,结果一定是这样的

res=[['ACAC'],['AACC'],['BACB'],['ABBC']]
我可以在另一个for循环中实现一个for循环,该循环进入每个列表,然后遍历每个字符串,并查看是否出现a和C,但我对如何保持幻影的顺序有点困惑,即a出现在C之前

或使用re:

来自@OlvinRoght的解决方案:

lst=[['AAAB'],['ACAC'],['CABA'],['AACC'],['BACB'],['BCAA'],['CBAA'],['ABBC']]
res = [i for i in lst if len(i[0]) > i[0].find('C') - i[0].find('A') > 0]

print(res)
或使用re:

来自@OlvinRoght的解决方案:

lst=[['AAAB'],['ACAC'],['CABA'],['AACC'],['BACB'],['BCAA'],['CBAA'],['ABBC']]
res = [i for i in lst if len(i[0]) > i[0].find('C') - i[0].find('A') > 0]

print(res)

尝试以下操作:编辑以考虑不包含字符“A”或“C”的列表

lst=[['AAAB'],['ACAC'],['CABA'],['AACC'],['BACB'],['BCAA'],['CBAA'],['ABBC']]

res = []

for lst_inner in lst:
    if ('A' in lst_inner[0] and 'C' in lst_inner[0] and
        lst_inner[0].find('A') < lst_inner[0].find('C')):
        
        res.append(lst_inner)

print(res)

尝试以下操作:编辑以考虑不包含字符“A”或“C”的列表

lst=[['AAAB'],['ACAC'],['CABA'],['AACC'],['BACB'],['BCAA'],['CBAA'],['ABBC']]

res = []

for lst_inner in lst:
    if ('A' in lst_inner[0] and 'C' in lst_inner[0] and
        lst_inner[0].find('A') < lst_inner[0].find('C')):
        
        res.append(lst_inner)

print(res)

扩展@andrej regex解决方案

import re
r = list(filter(lambda x: [i for i in x if re.findall('A.*C', i)], l))
非正则表达式、解决方案

def find_patt(lis):
    res =[]
    for sublis in lis:
        tmp =[]
        for ele in sublis:
            if ('A' in ele) and ('C' in ele):
                if ele.index('A')<len(ele)-ele[::-1].index('C'):
                    tmp.append(ele)
        if tmp!=[]:
            res.append(tmp)
            tmp =[]
    return res

lst=[['AAAB'],['ACAC'],['CABA'],['AACC'],['BACB'],['BCAA'],['CBAA'],['ABBC'], ['CAC']]

print(find_patt(lst))
# [['ACAC'], ['AACC'], ['BACB'], ['ABBC'], ['CAC']]

扩展@andrej regex解决方案

import re
r = list(filter(lambda x: [i for i in x if re.findall('A.*C', i)], l))
非正则表达式、解决方案

def find_patt(lis):
    res =[]
    for sublis in lis:
        tmp =[]
        for ele in sublis:
            if ('A' in ele) and ('C' in ele):
                if ele.index('A')<len(ele)-ele[::-1].index('C'):
                    tmp.append(ele)
        if tmp!=[]:
            res.append(tmp)
            tmp =[]
    return res

lst=[['AAAB'],['ACAC'],['CABA'],['AACC'],['BACB'],['BCAA'],['CBAA'],['ABBC'], ['CAC']]

print(find_patt(lst))
# [['ACAC'], ['AACC'], ['BACB'], ['ABBC'], ['CAC']]
试试这个

lst=['AAAB']、['ACAC']、['CABA']、['AACC']、['BACB']、['BCAA']、['CBAA']、['ABBC']] res=[] def清单\u字符串: index1=0 index2=0 对于列表字符串中的字母: 如果字母==“A”: index1=列表\字符串。indexletters 打破 对于列表字符串中的字母: 如果字母=='C': index2=列表\字符串。indexletters 打破 如果index1index2: 返回错误 其他: 返回错误 对于lst中的x: 字符串=x[0] string\u splitted=liststring 如果checkstring_被拆分: 附件二 印刷品 试试这个

lst=['AAAB']、['ACAC']、['CABA']、['AACC']、['BACB']、['BCAA']、['CBAA']、['ABBC']] res=[] def清单\u字符串: index1=0 index2=0 对于列表字符串中的字母: 如果字母==“A”: index1=列表\字符串。indexletters 打破 对于列表字符串中的字母: 如果字母=='C': index2=列表\字符串。indexletters 打破 如果index1index2: 返回错误 其他: 返回错误 对于lst中的x: 字符串=x[0] string\u splitted=liststring 如果checkstring_被拆分: 附件二 印刷品 “CAC”是否应包含在结果中?是否应将“CAC”包含在结果中?建议,在代码index1=list\U string.index'A',index2=list\U string.index'C'中,因此此处不需要使用for循环,因为您迭代到string,希望获得索引的最小值和索引的最大值,我建议您使用枚举函数,因为str.indexelement将始终提供元素建议的第一次出现的索引,在您的代码中,index1=list\u string.index'A',index2=list\u string.index'C',因此在这里使用for循环是不必要的,因为您迭代到string,希望获得索引的最小值和索引的最大值,我建议您使用枚举函数作为str。indexelement将始终提供元素首次出现的索引