Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Traverse sub-lists in nested list under conditions

$
0
0

I have to find the sub-lists in a nested list, in order to get a result list r, using Python.

  • Result list r is build from and initial list l.
  • 1st sub-list in r must contain starting point start, and intermediate sub-lists that are connected by elements from list p.
  • The last sub-list in r must contain ending point end.

It is like finding the proper sub-lists to traverse a nested list.

Here a small example.

#starting pointstart = 1#ending pointend = 9 #items to be used to link "start" and "end" points.p = [2, 4, 5]#complete nested listl = [    [0, 7],    [1, 2],    [8, 15, 19, 20],    [0, 6],    [2, 3, 5],    [10, 14],    [5, 8, 3, 4],    [4, 9, 6],    [14, 21],    [20, 9]]#result list, with "start" in sublist 0, "end" in sublist 3.r = [    [1, 2],#----^    [2, 3, 5],    [5, 8, 3, 4],    [4, 9, 6]#-------^]

thanks in advance for your help,

Best regards.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>