Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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 2.7 - Fatal编程技术网

在Python中的嵌套数组中搜索字符串中的字符串

在Python中的嵌套数组中搜索字符串中的字符串,python,python-2.7,Python,Python 2.7,我正在尝试使用一个基本函数来查找嵌套列表中另一个字符串中的字符串。但它无法找到相同的。有什么根本性的错误吗 import Tkinter, Tkconstants, tkFileDialog from Tkinter import * import subprocess import ttk import re def find_index_sub_string(needle,haystack): return [i for i, x in enumerate(haystack)

我正在尝试使用一个基本函数来查找嵌套列表中另一个字符串中的字符串。但它无法找到相同的。有什么根本性的错误吗

import Tkinter, Tkconstants, tkFileDialog
from   Tkinter import *
import subprocess
import ttk
import re

def find_index_sub_string(needle,haystack):
    return [i for i, x in enumerate(haystack) if needle in x]

mc_pool = [['fruit,apple', '1,appl_c', [''], '1,am', '1,as', ['fruit,apple,am-fruit,apple,as-rocky,1'], '/abc/bb/ccc/dd/e', ['1,aa', ['fruit,apple,aa', 'aa', 1, [], [], [], [], [], []]]], ['apple,appl_c', '', [''], '1,mm', '1,ms', ['apple,appl_c,mm-apple,appl_c,ms-rambo,1'], '/aa/bbbd/cc/dddd', ['']]]
mc_pool_cln = [['fruit,apple', '1,appl', ['fruit,apple,mas', 'mas', '5'], '1,am', '1,as', ['fruit,apple,am-fruit,apple,as-rocky,1'], '/abc/bb/ccc/dd/e', ['1,aa', ['fruit,apple,aa', 'aa', 1, [], [], [], [], [], []]]], ['apple,appl', '', [''], '1,mm', '1,ms', ['apple,appl,mm-apple,appl,ms-rambo,1'], '/aa/bbbd/cc/dddd', ['']]]

mc_idx = find_index_sub_string('_c', mc_pool)
print mc_idx 

mc_idx = find_index_sub_string('_c', mc_pool_cln)
print mc_idx 
更新:

添加了另一个数组mc_pool_cn,该数组不包含任何字符串,即_c。。 预期的输出是[0,1],[1,0],[1,5]。。i、 e.与出现的子字符串的相应索引位置相似。。
您正在搜索精确匹配,而不是子字符串

因为您有递归嵌套的列表,所以需要使用递归函数来搜索任何深度

def match_substring_recursive(needle, haystack):
    if isinstance(haystack, str):
        return needle in haystack
    else:
        return any(match_substring_recursive(needle, x) for x in haystack)

def find_index_sub_string(needle, haystack):
    return [i for i, x in enumerate(haystack) if match_substring_recursive(needle, x)]

问题是列表中有列表和字符串。 您需要区分这两种类型

职能:

def search(needle, haystack):
    # If haystack is string
    if type(haystack) is str and needle in haystack:
        return [haystack]
    # Else (haystack is a list not empty)
    elif type(haystack) is list and len(haystack) > 0:
        # Recursion on first element and others elements on haystack
        return search(needle, haystack[0]) + search(needle, haystack[1:])
    # default value
    return []

mc_pool = [['fruit,apple', '1,appl_c', [''], '1,am', '1,as', ['fruit,apple,am-fruit,apple,as-rocky,1'], '/abc/bb/ccc/dd/e', ['1,aa', ['fruit,apple,aa', 'aa', 1, [], [], [], [], [], []]]], ['apple,appl_c', '', [''], '1,mm', '1,ms', ['apple,appl_c,mm-apple,appl_c,ms-rambo,1'], '/aa/bbbd/cc/dddd', ['']]]
mc_idx = search('_c', mc_pool)

print (mc_idx) 
# ['1,appl_c', 'apple,appl_c', 'apple,appl_c,mm-apple,appl_c,ms-rambo,1']

希望这对您有所帮助。

感谢您的投入。。但是如果变量存在,这将返回变量,对吗?不是索引值。当前代码返回列表中以指针作为子字符串的所有字符串。您想要的输出是什么?主要是返回列表的索引,其中包含字符串中所需的子字符串,而不是返回整个字符串。我不确定是否理解,因为这是一个递归搜索。您能否共享此示例中的预期输出?如果列表中有任何字符串,您将按此方式打印该字符串。但是我试图打印那个字符串所在的索引。例如[0,1],即字符串存在于位置1的数组0中,类似于..感谢您的输入。。很快就会怀疑,这基本上返回了它存在的顶级索引,对吗?但是我想知道我是否能得到元素所在的确切索引?例如,元素存在于数组[0]中,位于位置[1]中的数组内。。但是这个递归过程只打印顶级数组号,对吗?如果数组中没有任何带有“_c”的字符串作为TypeError,递归函数将失败:“int”对象不可iterable。您在递归过程中检查str是bcoz吗?@Vimo它假定所有内容都是字符串或字符串列表。请编辑问题并显示所需结果。我以为您只需要顶级索引,因为这是您的函数返回的。您已经用另一个不包含任何_c.的数组mc_pool_cln更新了查询。。以及打印子字符串所在的相应索引。。