By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,402 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,402 IT Pros & Developers. It's quick & easy.

copy an iterable class object

badech
P: 16
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
Expand|Select|Wrap|Line Numbers
  1. def copieIterative(l):
  2.     global l1,der,lcopie
  3.     l1=l
  4.     lcopie=None
  5.     if l1!=None :
  6.         lcopie=ajoute_Tete(lcopie,l1.val)
  7.         der=lcopie
  8.         l1=l1.suiv
  9.         while l1 != None :
  10.             der=ajoute_Tete(der,l1.val)
  11.             der=der.suiv
  12.             l1=l1.suiv
  13.     return lcopie
but it doesn't work :(
it returns this
Expand|Select|Wrap|Line Numbers
  1. >>> l=None
  2. >>> for i in range(7) :
  3.     l=ajoute_Tete(l,randrange(12,68767,5))
  4. >>> l
  5. 7122 32857 38922 23662 20242 26207 8617  
  6. >>> lcopie=None
  7. >>> der=None
  8. >>> l1=None
  9. >>> v=copieIterative(l)
  10. >>> v
  11. 7122           <----- the problem is here , it doesn't copie the whole list , just the first element
any suggestion ?
thanks a lot

PS:i have these two functions :
Expand|Select|Wrap|Line Numbers
  1. class Noeud:
  2.     def __init__(self,valeur,suivant=None):
  3.         self.val = valeur
  4.         self.suiv = suivant
  5.     def __repr__(self):
  6.         res = str(self.val)+" "
  7.         if (self.suiv != None):
  8.             res = res + str(self.suiv)
  9.         return res
  10.     def __lt__(self, autre):
  11.         return self.val < autre.val
  12.  
  13. def ajoute_Tete(L, x):
  14.     return Noeud(x, L)
Jan 1 '07 #1
Share this Question
Share on Google+
11 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
'l' is an instance. To copy the list displayed by print:
Expand|Select|Wrap|Line Numbers
  1. >>> print l
  2. 19337 68037 36652 59487 28882 22032 50767 10717 30877 64142 
  3. >>> print type(l)
  4. <type 'instance'>
  5. >>> copiedList = repr(l).split()
  6. >>> print "Copied List: %s" % (copiedList)
  7. Copied List: ['19337', '68037', '36652', '59487', '28882', '22032', '50767', '10717', '30877', '64142']
  8. >>> print type(copiedList)
  9. <type 'list'>
  10. >>> print map(int, copiedList)
  11. [19337, 68037, 36652, 59487, 28882, 22032, 50767, 10717, 30877, 64142]
  12. >>> print type(map(int, copiedList)[0])
  13. <type 'int'>
Jan 1 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
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'):
Expand|Select|Wrap|Line Numbers
  1. def copyinstList(a):
  2.     b = repr(a).split()
  3.     return map(int, b)
HTH
Jan 1 '07 #3

badech
P: 16
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
Jan 1 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. from random import randrange
  2.  
  3. class Noeud(object):
  4.  
  5.     def __init__(self, valeur, suivant=None):
  6.         self.val = valeur
  7.         self.suiv = suivant
  8.         self.listUpdate()
  9.  
  10.     def __repr__(self):
  11.         res = str(self.val)+" "
  12.         if (self.suiv != None):
  13.             res += str(self.suiv)
  14.         return res
  15.  
  16.     def __lt__(self, autre):
  17.         return self.val < autre.val
  18.  
  19.     def listUpdate(self):
  20.         if self.suiv == None:
  21.             self.list = [self.val, ]
  22.         else:
  23.             self.list = [self.val, ] + self.suiv.list
  24.  
  25. def ajoute_Tete(L, x):
  26.     return Noeud(x, L)
  27.  
  28. a = None
  29. for i in range(10) :
  30.     a = ajoute_Tete(a,randrange(12,68767,5))
  31.  
  32. def copieIterative(z):
  33.     return z.list
  34.  
  35. v = copieIterative(a)
  36.  
  37. >>> print a
  38. 35667 8742 52832 7697 66512 33497 56017 31472 18857 14202 
  39.  
  40. >>> print v
  41. [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.
Jan 2 '07 #5

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

bartonc
Expert 5K+
P: 6,596
This may give you some ideas:
Expand|Select|Wrap|Line Numbers
  1. from random import randrange
  2.  
  3. class Noeud(object):
  4.  
  5.     def __init__(self, valeur, suivant=None):
  6.         self.val = valeur
  7.         self.suiv = suivant
  8.         self.listUpdate()
  9.  
  10.     def __repr__(self):
  11.         res = str(self.val)+" "
  12.         if (self.suiv != None):
  13.             res += str(self.suiv)
  14.         return res
  15.  
  16.     def __lt__(self, autre):
  17.         return self.val < autre.val
  18.  
  19.     def listUpdate(self):
  20.         if self.suiv == None:
  21.             self.list = [self.val, ]
  22.         else:
  23.             self.list = [self.val, ] + self.suiv.list
  24.  
  25. def ajoute_Tete(L, x):
  26.     return Noeud(x, L)
  27.  
  28. a = None
  29. for i in range(10) :
  30.     a = ajoute_Tete(a,randrange(12,68767,5))
  31.  
  32. def copieIterative(z):
  33.     return z.list
  34.  
  35. v = copieIterative(a)
  36.  
  37. >>> print a
  38. 35667 8742 52832 7697 66512 33497 56017 31472 18857 14202 
  39.  
  40. >>> print v
  41. [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".
Jan 2 '07 #7

badech
P: 16
hello
finally i secceded to write the function here is the code :
Expand|Select|Wrap|Line Numbers
  1. def copieIterative2(L):
  2.     L1=L
  3.     lcopie=None
  4.     while L1!=None :
  5.         lcopie=ajoute_Tete(lcopie,L1.val)
  6.         L1=L1.suiv
  7.     return lcopie
which works but it returns the list reversed
can you tell me why it reverse the list please ?
thank you
Jan 2 '07 #8

badech
P: 16
no answer ? :(
please help me
Jan 3 '07 #9

bvdet
Expert Mod 2.5K+
P: 2,851
hello
finally i secceded to write the function here is the code :
Expand|Select|Wrap|Line Numbers
  1. def copieIterative2(L):
  2.     L1=L
  3.     lcopie=None
  4.     while L1!=None :
  5.         lcopie=ajoute_Tete(lcopie,L1.val)
  6.         L1=L1.suiv
  7.     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 'copieIterative2()' 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.
Jan 3 '07 #10

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Jan 4 '07 #11

badech
P: 16
question solved by bvdet
thank you bvdet
i'll post the answer later
thank you :)
Jan 6 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.