Removing multidimensional lists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • m6s
    New Member
    • Aug 2007
    • 55

    Removing multidimensional lists

    Hello to all,
    I am having trouble removing an item from list, which the item is a list by itself.
    <code>
    for self.line in self.filtered:
    if self.appid:
    if self.line[0] <> self.appid:
    print RED + " ".join( map ( lambda x:str(x), self.filtered[self.filtered.i ndex( self.line) ] )) + RESET
    self.filtered.r emove( self.line.index (self.line[0] ) )
    else:
    print "F: " + str(self.line)
    self.ci+=1
    </code>
    I am trying either to remove...but fails or use the del... No chance
    Imaging each item in the list is a list itself of 8 fields.

    The problem is based on that when loading the data...from csv, the reader's data are passed by reference. Removing with del, gives problem because it has the reference.

    In the end what I wish to do is to subtract elements from the list.
    I am overload...I don't know if I gave it clear...
    Hope somebody helps...

    Thank yu
  • kaarthikeyapreyan
    New Member
    • Apr 2007
    • 106

    #2
    To remove a particular Element form an multi dimensional array which is a list
    Code:
    >>> mdlist=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
    >>> mdlist.pop(0)
    [1, 2, 3]
    >>> print mdlist
    [[4, 5, 6], [7, 8, 9], [10, 11, 12]]
    To remove a particular Element form an multi dimensional array which is a member of the list(sub element) in the list
    Code:
    >>> mdlist[0].pop(0)
    4
    >>> print mdlist
    [[5, 6], [7, 8, 9], [10, 11, 12]]
    >>>

    Comment

    • m6s
      New Member
      • Aug 2007
      • 55

      #3
      First, thank you for your answer...
      Yes this seems to be the theory, the real problem comes when I want to loop through the elements of the list, and remove items. I think it has nothing to do with multidimensiona l, really.
      suppose we have one list, I want to iterate through all the elements remove by index and leave the list with less items than the start.

      I used the copy module, and tried copy.deepcopy, in order to build a new list-clone.

      Retrieving all the elements of the close I tried to remove from the original list.
      <code>
      import copy
      self.tmp = copy.deepcopy( self.filtered )
      for self.line in self.tmp:
      if self.appid:
      try:
      if self.line[0].find( self.appid ) == -1:
      print RED + str(self.line[0]) + RESET
      del self.filtered[ self.ci ]
      else:
      print GREEN + str(self.line[0] ) + RESET
      except:
      ""
      self.ci+=1
      sys.exit(23)
      </copy>
      If I remove the try: except thing, I have this :
      Traceback (most recent call last):
      File "./preybill.py", line 291, in ?
      s.getCSV( argd, p )
      File "./preybill.py", line 219, in getCSV
      del self.filtered[ self.ci ]
      IndexError: list assignment index out of range

      The fun part is that the element is really removed. I tried also in console to emulate the script and definetely removes the element.

      I worked many hours to isolate the problem because in the first place it seems that it was the multi-thing...wrong of me ....

      Comment

      • m6s
        New Member
        • Aug 2007
        • 55

        #4
        This link
        http://www.python.org/search/hypermail/python-recent/0678.html

        finally did resolve the case.
        The only thing I used the copy module.

        Comment

        Working...