+ Reply to Thread
Results 1 to 6 of 6

Thread: removing, replacing and moving contents in a list

  1. #1

    removing, replacing and moving contents in a list

    Hi everyone again, yet another question on lists

    consider this list:

    Python Code:
      mylist=[]
      mylist.append(['B2', ['C3', 'C4', ['D5', 'D6']], 'B7','B8', ['C9', [[['F10'],'E11'],'D12']]])

    I want to iterate through all the contents and for every string that contains 'B' to remove them and place all of them in a new sublist one level deeper in a way that the list prints
    Python Code:
      [['B2','B7','B8'], ['C3', 'C4', ['D5', 'D6']], ['C9', [[['F10'],'E11'],'D12']]]
    I've using list.remove(item) while in a 'for item in list' loop and it reduces the index by one messing up everything.

    so i decided to create a new list with the Strings positioned the way i want:

    Python Code:
      def NewList(thelist):
          sublist = []
          sublist2=[]
          newlist= []
          for item in thelist:
              if hasattr(item,"__iter__"):
                  for subitem in item:
                      sublist.append(subitem)
              elif "B" in item:
                      sublist2.append(item)
              else:
                  newlist.append(item)
                 
          newlist.append(sublist2)
         
          if sublist: newlist.append(NewList(sublist))
         
          return newlist

    but this returns:

    python Code:
      [[], [['B2', 'B7', 'B8'], ['C3', 'C4', 'C9', [], ['D5', 'D6', 'D12', [], ['E11', [], ['F10', []]]]]]]

    and i don't understand what are the empty slots or the extra brackets...

    any help appreciated !

    regards,

    Pav
    Last edited by pavlos03; 10-20-2012 at 06:10 PM.

  2. #2
    Senior Member Steve Baer's Avatar
    Join Date
    Apr 2010
    Location
    Seattle
    Posts
    1,463
    Blog Entries
    19
    Hi Pav,
    I would recommend posting this question on stackoverflow.com. It is a generic question about python lists that someone there is probably going to do a much better job at answering than I can.

    You can probably figure out what is going on by placing a breakpoint at the beginning of your function and stepping through the code to understand why the empty lists are being added.
    Thanks,
    -Steve

  3. #3
    Hi Pavlos03,

    Like Steve said, StackOverflow is really a good resource for these kind of questions. It's amazing. On such questions you might get an answer within a few minutes.

    In your case the empty brackets are empty lists. You can avoid them by adding an if statement before newlist.append. But i am not sure if that gets you the desired return :
    python Code:
      if sublist2: newlist.append(sublist2)
    That is the beauty of Pythons dynamic typing: after an if statement an []empty list, ()empty tuple, 0 , 0.0 and "" an empty string will all evaluate as False.

    Steve, I hope you read this,
    how about starting a page using the stackoverflow model for RhinoPython ?
    like one of those:
    http://stackexchange.com/sites#
    here for new ones:
    http://area51.stackexchange.com/
    Last edited by Goswin; 10-24-2012 at 08:31 PM.

  4. #4
    Senior Member Steve Baer's Avatar
    Join Date
    Apr 2010
    Location
    Seattle
    Posts
    1,463
    Blog Entries
    19
    I think you need an awful lot of people to push to get a new site on stackexchange. I've thought about doing a better job of using stackoverflow for Rhino SDK related questions in the past, but never really figured out a good approach for this.

  5. #5
    What if the proposal for a stack exchange site was more related to 3d modelling application programming? I spend a lot of time on rhino but I spend more time programming for other applications in the same general domain. I am sure that a lot of people that follow this forum would be interested in a stack exchange site that included rhino and the other applications we have to mess with daily.

  6. #6
    Hi Steve, It does not seem to be so difficult to launch a Stack site, but aparently you need 15 questions per day to get a site through beta. for example: http://area51.stackexchange.com/proposals/5022/3d-graphics-modeling-applications was closed.
    Currently there are only 2-3 per day on RhinoPython but maybe the amount of questions will increase after the launch of Rh5.
    I think a site could cover mainly RhinoPython and Common but maybe C++ , vbscript and interaction with other CAD packages( Revit..) too.
    Was David considering it for Grashopper ?

+ 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