0

I am trying to have a basic function to find a string inside another string in a nested list. But its unable to find the same. Is anything fundamentally wrong ?

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 

Update:

  1. Have added another array mc_pool_cn which doesn't contain any of the strings i.e. _c..
  2. Was expecting the output like, [[0,1], [1,0], [1,5]].. i.e. like the respective index location of the sub string that appears..

2 Answers 2

1

You're searching for an exact match, not a substring.

And since you have recursively nested lists, you need to use a recursive function to search to any depth.

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)]
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your inputs.. Quick doubt, this basically return the top level index where it exits right? But I was wondering If i can get the exact index where the element exists as well ? For eg, the element exists in array [0], inside that in location [1].. But this recursive process prints only the top level array number right?
The recursive function fails if there isn't any string with '_c' in the arrays as "TypeError: 'int' object is not iterable". Is it bcoz you check for str in the recursive process ?
@Vimo It assumes everything is either a string or a list of strings.
Please edit the question and show the desired result. I thought you just wanted the top-level indexes, since that's what your function returns.
Have already update the query with the another array mc_pool_cln which doesn't contain any _c.. And also to print the respective index where the substring exists..
|
0

The problem is that there are list and string in the list. You need to distinguish both types.

The function:

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']

Hope that helps you.

5 Comments

Thanks for your inputs.. But this will be returning the variables as such if it exists right? not the index value.
The current code return all string of the list having needle as a sub-string. What is your desired output ?
Was mainly trying to return the index of the list which contains the required sub string in the string, not to return the string as a whole..
I'm not sure to understand since this is a recursive search. Can you share the expected output on this example ?
You are printing that string as such if there is any in the list. But I was trying to print the index where that particular string exists. For eg [0,1] i.e. the string exists in the array 0 in the location 1, something like that..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.