+ Reply to Thread
Page 2 of 2
FirstFirst 1 2
Results 11 to 14 of 14

Thread: iterating through nested lists - flattening

  1. #11
    Quote Originally Posted by Steve Baer View Post
    I'm sure I could answer this if you gave me a sample of input and what the output order you want is
    Hi Steve and hi everyone else. thanks for all your replies! To clarify what i want I put together the code you wrote me Steve with a list 'array' and the result of the code below:

    Python Code:
      def Main():
         
          array = []
          array.append("A1")
          array.append("A2")
          array.append([["C3","C4"], ["C5","C6"]])
          array.append(['A7'])
          array.append(['B8', 'B9'])
          array.append(['A10', ['B11', 'B12', ['C13', 'C14']]])
         
          WalkList(array)
         
         
      def WalkList(thelist):
          for item in thelist:
              if type(item) == type(list()):
                  print "nested List found"
                  WalkList(item)
              elif item is tuple:
                  print "Tuple"
                  WalkList(item)
              else:
                  print item

    so if you run the above code it gives you:

    A1
    A2
    C3
    C4
    C5
    C6
    A7
    B8
    B9
    A10
    B11
    B12
    C13
    C14


    So as you can see it prints out the contents of the list one by one depending on which one comes first, and if its a list then it iterates through that list until it reaches contents and not sublist.

    The letter (A,B,C) infront of the contents notes the sublist 'level'. According to that I would like to find a way to sort the contents that it prints out all 'As' first, then all 'Bs' then 'Cs' etc. Then the result should be:

    A1
    A2
    A7
    A10
    B8
    B9
    B11
    B12
    C3
    C4
    C5
    C6
    C13
    C14


    Thanks everyone for your replies, I hope i clarified my question enough !

    regards,

    Pav

  2. #12
    Hi Pav,

    I saw that my previous version did not sort as you suggested. If you assume this nesting:
    python Code:
      def Main():   
          array = []
          array.append("A1")
          array.append("A2")
          array.append([["C3","C4"], ["C5","C6"]])
          array.append(['B7'])
          array.append(['B8', 'B9'])
          array.append("A3")
          array.append(['B10', ['C11', 'C12', ['D13', 'D14']]])
         
          WalkList(array)
      Main()

    then this should work:

    python Code:
      def WalkList(thelist):   
          sublist = []   
          for item  in thelist:
              if hasattr(item,"__iter__"): # test if it is a list, tuples, .net-arrays or other collections. but not Strings
                  for subitem in item:
                      sublist.append(subitem) #save for iterating recursively after printing this level
              else:
                  print item
       
          if sublist: WalkList(sublist) # recurse if subliste is not empty
       

    prints:

    A1
    A2
    A3
    B7
    B8
    B9
    B10
    C3
    C4
    C5
    C6
    C11
    C12
    D13
    D14

  3. #13
    Hi Goswin,

    thanks for your reply !

    first it seems that my array contents naming wasn't correct and you fixed it, thanks. Apparently array.append("A") and array.append(["A"]) is not the same !, with the latter already being a sublist with just one string.

    what you wrote seems to works for now, Thanks!

    my only worry is the appending to the sublist and not adding on specific locations. I'm not sure but if we have a list of points instead of strings and we rearrange them all the time in sublists, then if the 'sublist' keeps getting appended it means that we'll get doubles , triples and so on...? or not! I'll see how it works and get back to you.

    thanks again

  4. #14
    so here's what i've come up with for saving the flatten list and returning it to the Main()

    Python Code:
      def Main():
         
          array = []
          array.append("A1")
          array.append("A2")
          array.append([["C3","C4"], ["C5","C6"]])
          array.append(['B7'])
          array.append('A8')
          array.append(['B9', 'B10'])
          array.append(['B11', ['C12', 'C13', ['D14', 'D15']]])
         
          flattenlist = []
          flattenlist.extend(WalkList(array))
         
          if flattenlist:
              for item in flattenlist:
                  print item
       
      def WalkList(thelist):   
          sublist = []   
          flattenlist = []
          for item  in thelist:
              if hasattr(item,"__iter__"):
                  for subitem in item:
                      sublist.append(subitem)
              else:
                  print item
                  flattenlist.append(item)
       
          if sublist: flattenlist.extend(WalkList(sublist))
         
          return flattenlist
       
      Main();

    I had some issues with list.append and list.extend but I think i solved them (can't say i really understood how!). The flattenlist returned though is flattened and prints the same like the printed result of WalkList function.

    thanks again !

    Pav

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts