copy an iterable class object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • badech
    New Member
    • Jan 2007
    • 16

    #1

    copy an iterable class object

    Hello every ody and happy new year
    i need some help
    i want to write a function which copies a list
    here is the code
    Code:
    def copieIterative(l):
        global l1,der,lcopie
        l1=l
        lcopie=None
        if l1!=None :
            lcopie=ajoute_Tete(lcopie,l1.val)
            der=lcopie
            l1=l1.suiv
            while l1 != None :
                der=ajoute_Tete(der,l1.val)
                der=der.suiv
                l1=l1.suiv
        return lcopie
    but it doesn't work :(
    it returns this
    Code:
    >>> l=None
    >>> for i in range(7) :
    	l=ajoute_Tete(l,randrange(12,68767,5))
    >>> [B]l
    7122 32857 38922 23662 20242 26207 8617 [/B] 
    >>> lcopie=None
    >>> der=None
    >>> l1=None
    >>> v=copieIterative(l)
    >>> [B]v
    7122           <----- the problem is here , it doesn't copie the whole list , just the first element[/B]
    any suggestion ?
    thanks a lot

    PS:i have these two functions :
    Code:
    class Noeud:
        def __init__(self,valeur,suivant=None):
            self.val = valeur
            self.suiv = suivant
        def __repr__(self):
            res = str(self.val)+" "
            if (self.suiv != None):
                res = res + str(self.suiv)
            return res
        def __lt__(self, autre):
            return self.val < autre.val
    
    def ajoute_Tete(L, x):
        return Noeud(x, L)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    'l' is an instance. To copy the list displayed by print:
    Code:
    >>> print l
    19337 68037 36652 59487 28882 22032 50767 10717 30877 64142 
    >>> print type(l)
    <type 'instance'>
    >>> copiedList = repr(l).split()
    >>> print "Copied List: %s" % (copiedList)
    Copied List: ['19337', '68037', '36652', '59487', '28882', '22032', '50767', '10717', '30877', '64142']
    >>> print type(copiedList)
    <type 'list'>
    >>> print map(int, copiedList)
    [19337, 68037, 36652, 59487, 28882, 22032, 50767, 10717, 30877, 64142]
    >>> print type(map(int, copiedList)[0])
    <type 'int'>

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      I have one more suggestion. Do not use 'l' (lower case 'L') in variable names unless it is obvious as in 'lucky_number'. Many times it is difficult to distinguish between 'l' and '1', depending on the font. Following is a function that will return a list of integers from your instance 'l' (substituting 'a' for 'l'):
      Code:
      def copyinstList(a):
          b = repr(a).split()
          return map(int, b)
      HTH

      Comment

      • badech
        New Member
        • Jan 2007
        • 16

        #4
        thank you for your answers but i must write this function by myself ( i musn't use the predefinied functions ) (it's an exercise )
        thanks

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by badech
          thank you for your answers but i must write this function by myself ( i musn't use the predefinied functions ) (it's an exercise )
          thanks
          This may give you some ideas:
          Code:
          from random import randrange
          
          class Noeud(object):
              
              def __init__(self, valeur, suivant=None):
                  self.val = valeur
                  self.suiv = suivant
                  self.listUpdate()
          
              def __repr__(self):
                  res = str(self.val)+" "
                  if (self.suiv != None):
                      res += str(self.suiv)
                  return res
              
              def __lt__(self, autre):
                  return self.val < autre.val
              
              def listUpdate(self):
                  if self.suiv == None:
                      self.list = [self.val, ]
                  else:
                      self.list = [self.val, ] + self.suiv.list
                                  
          def ajoute_Tete(L, x):
              return Noeud(x, L)
          
          a = None
          for i in range(10) :
              a = ajoute_Tete(a,randrange(12,68767,5))
          
          def copieIterative(z):
              return z.list
              
          v = copieIterative(a)
          
          >>> print a
          35667 8742 52832 7697 66512 33497 56017 31472 18857 14202 
          
          >>> print v
          [35667, 8742, 52832, 7697, 66512, 33497, 56017, 31472, 18857, 14202]
          Your code returns an instance with one element in the string. I do not know what your assignment rules are, so I added a class method to accumulate a list that you can readily access.

          Comment

          • badech
            New Member
            • Jan 2007
            • 16

            #6
            thank you for your answer , i'll see after ( it's now 3:07 so ...)
            thanks........

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by bvdet
              This may give you some ideas:
              Code:
              from random import randrange
              
              class Noeud(object):
                  
                  def __init__(self, valeur, suivant=None):
                      self.val = valeur
                      self.suiv = suivant
                      self.listUpdate()
              
                  def __repr__(self):
                      res = str(self.val)+" "
                      if (self.suiv != None):
                          res += str(self.suiv)
                      return res
                  
                  def __lt__(self, autre):
                      return self.val < autre.val
                  
                  def listUpdate(self):
                      if self.suiv == None:
                          self.list = [self.val, ]
                      else:
                          self.list = [self.val, ] + self.suiv.list
                                      
              def ajoute_Tete(L, x):
                  return Noeud(x, L)
              
              a = None
              for i in range(10) :
                  a = ajoute_Tete(a,randrange(12,68767,5))
              
              def copieIterative(z):
                  return z.list
                  
              v = copieIterative(a)
              
              >>> print a
              35667 8742 52832 7697 66512 33497 56017 31472 18857 14202 
              
              >>> print v
              [35667, 8742, 52832, 7697, 66512, 33497, 56017, 31472, 18857, 14202]
              Your code returns an instance with one element in the string. I do not know what your assignment rules are, so I added a class method to accumulate a list that you can readily access.
              Bilingual coding... I'm impressed. I confess that I need english identifiers in order to have a clue about somebody else's code. You are indeed worthy of your new title and that of "expert".

              Comment

              • badech
                New Member
                • Jan 2007
                • 16

                #8
                hello
                finally i secceded to write the function here is the code :
                Code:
                def copieIterative2(L):
                    L1=L
                    lcopie=None
                    while L1!=None :
                        lcopie=ajoute_Tete(lcopie,L1.val)
                        L1=L1.suiv
                    return lcopie
                which works but it returns the list reversed
                can you tell me why it reverse the list please ?
                thank you

                Comment

                • badech
                  New Member
                  • Jan 2007
                  • 16

                  #9
                  no answer ? :(
                  please help me

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    Originally posted by badech
                    hello
                    finally i secceded to write the function here is the code :
                    Code:
                    def copieIterative2(L):
                        L1=L
                        lcopie=None
                        while L1!=None :
                            lcopie=ajoute_Tete(lcopie,L1.val)
                            L1=L1.suiv
                        return lcopie
                    which works but it returns the list reversed
                    can you tell me why it reverse the list please ?
                    thank you
                    You are passing the integer arguments to your function in reverse order. The first argument 'L1.val' in 'copieIterative 2()' was the last argument passed in the instance you are trying to copy. Adding another iteration may work. Accumulate a list of integers, execute a 'for i in int_list:' and the instance can be recreated in the correct order.

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      Originally posted by bartonc
                      Bilingual coding... I'm impressed. I confess that I need english identifiers in order to have a clue about somebody else's code. You are indeed worthy of your new title and that of "expert".
                      Thanks Barton! English variable names and a few comments would have helped. I was a little confused but fortunately the exercise was not too difficult.

                      Comment

                      • badech
                        New Member
                        • Jan 2007
                        • 16

                        #12
                        question solved by bvdet
                        thank you bvdet
                        i'll post the answer later
                        thank you :)

                        Comment

                        Working...