Search a nested list against items from alternative list, returning subsets that contain partial match

StackOverflow https://stackoverflow.com/questions/23183781

  •  06-07-2023
  •  | 
  •  

Question

Say I have two lists such as:

NestedLst = ['1234567', 'abc456789', ['cde678945']]
SearchLst = ['123', 'cde']

How do I search the nested list for any partial matches with any of the items of the search list and return for each search item the subset list. So in this case I want the output to be:

['1234567', 'abc456789', ['cde678945']] and ['cde678945'] 

I have tried doing the below:

    indices = []

for eachItem in SearchLst:
    for i, elem in enumerate(NestedLst):
        if eachItem in elem:
            indices.append(i)

print indices

but this always returns [0]. Any help would be much appreciated. As I am new to Python a full explanation of the code would be very helpful for me to learn.

Thankyou

Below is an example nested list in practice:

[['BMNH833953:0.16529463651919140688', [[['BMNH833883:0.22945757727367316336', ['BMNH724182a:0.18028180766761139897', ['BMNH724182b:0.21469677818346077913', 'BMNH724082:0.54350916483644962085'], ':0.00654573856803835914'], ':0.04530853441176059537'], ':0.02416511342888815264', [[['BMNH794142:0.21236619242575086042', ['BMNH743008:0.13421900772403019819', 'BMNH724591:0.14957653992840658219'], ':0.02592135486124686958'], ':0.02477670174791116522', 'BMNH703458a:0.22983459269245612444'], ':0.00000328449424529074', 'BMNH703458b:0.29776257618061197086'], ':0.09881729077887969892'], ':0.02257522897558370684', 'BMNH833928:0.21599133163597591945'], ':0.02365043128986757739', 'BMNH724053:0.16069861523756587274'], ':0.0;\n']
Was it helpful?

Solution

You can do that using a method and recursion:

NestedLst = ['1234567', 'abc456789', ['cde678945']]
SearchLst = ['123', 'cde']

def get_list(a,b):
    for i in b:
        if type(i)==list:          #if this is a nested list, do the same function for the contents of that list, returning that list if it meets the conditions.
            return get_list(a,i)
        elif a in i:               #if element in list is not a nested list, just do normal check for containment using in
            return b
    return []

x = [get_list(i,NestedLst) for i in SearchLst]  #just calls the function for all search terms

for k in x:
    print k

[OUTPUT]
['1234567', 'abc456789', ['cde678945']]
['cde678945']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top