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


say have 2 lists such as:

nestedlst = ['1234567', 'abc456789', ['cde678945']] searchlst = ['123', 'cde'] 

how search nested list partial matches of items of search list , return each search item subset list. in case want output be:

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

i have tried doing below:

    indices = []  eachitem in searchlst:     i, elem in enumerate(nestedlst):         if eachitem in elem:             indices.append(i)  print indices 

but returns [0]. appreciated. new python full explanation of code helpful me learn.

thankyou

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

you can using method , recursion:

nestedlst = ['1234567', 'abc456789', ['cde678945']] searchlst = ['123', 'cde']  def get_list(a,b):     in b:         if type(i)==list:          #if nested list, same function contents of list, returning list if meets conditions.             return get_list(a,i)         elif in i:               #if element in list not nested list, normal check containment using in             return b     return []  x = [get_list(i,nestedlst) in searchlst]  #just calls function search terms  k in x:     print k  [output] ['1234567', 'abc456789', ['cde678945']] ['cde678945'] 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -