assign the values to each members of a large list of class objects

Thread Solved
Reply

Join Date: Oct 2007
Posts: 30
Reputation: jliu66 is an unknown quantity at this point 
Solved Threads: 0
jliu66 jliu66 is offline Offline
Light Poster

assign the values to each members of a large list of class objects

 
0
  #1
Nov 5th, 2007
Hi,

I have a question regard to assign the values to each memebrs of the class objects because I have a large list of class objects. Can I do the following? If not, how can I achieve the same thing? Thanks in advance.

class person():
def __init__(self):
self.name = ' '
self.job = ' '
self.age = 0

# define a list of class objects
personList = []

personList[0].name = 'abc'
personList[0].job = 'worker'
personList[0].age = 20
personList[1].name = 'efg'
personList[1].job = 'student'
personList[1].age = 22
personList[2].name = 'hlk'
personList[2].job = 'doctor'
personList[2].age = 25
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 149
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: assign the values to each members of a large list of class objects

 
0
  #2
Nov 5th, 2007
Well, wrap your posts in [ code = Python ] [/code] tags for greater clarity.

My favorite way to do this would be to pull the information out of tuples like this:

  1. class person():
  2. def __init__(self):
  3. self.name = ' '
  4. self.job = ' '
  5. self.age = 0
  6.  
  7. people = [('abc','worker',20),
  8. ('efg', 'student',22),
  9. ('hlk', 'doctor', 25)] # yeah, right!
  10.  
  11. personList = []
  12.  
  13. for name,job,age in people: # this unpacks each tuple in the list.
  14. tmp = person()
  15. tmp.name = name
  16. tmp.job = job
  17. tmp.age = age
  18. personList.append(tmp)

Now, if you're willing to modify the __init__ a bit, then you can write even cleaner code:

  1. class person():
  2. def __init__(self,name='',job='',age=0):
  3. self.name = name
  4. self.job = job
  5. self.age = age
  6.  
  7. people = [('abc','worker',20),
  8. ('efg', 'student',22),
  9. ('hlk', 'doctor', 25)] # yeah, right!
  10.  
  11. personList = []
  12.  
  13. for name,job,age in people:
  14. personList.append(Person(name,job,age))

Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: jliu66 is an unknown quantity at this point 
Solved Threads: 0
jliu66 jliu66 is offline Offline
Light Poster

Re: assign the values to each members of a large list of class objects

 
0
  #3
Nov 6th, 2007
Originally Posted by jrcagle View Post
Well, wrap your posts in [ code = Python ] [/code] tags for greater clarity.

My favorite way to do this would be to pull the information out of tuples like this:

  1. class person():
  2. def __init__(self):
  3. self.name = ' '
  4. self.job = ' '
  5. self.age = 0
  6.  
  7. people = [('abc','worker',20),
  8. ('efg', 'student',22),
  9. ('hlk', 'doctor', 25)] # yeah, right!
  10.  
  11. personList = []
  12.  
  13. for name,job,age in people: # this unpacks each tuple in the list.
  14. tmp = person()
  15. tmp.name = name
  16. tmp.job = job
  17. tmp.age = age
  18. personList.append(tmp)

Now, if you're willing to modify the __init__ a bit, then you can write even cleaner code:

  1. class person():
  2. def __init__(self,name='',job='',age=0):
  3. self.name = name
  4. self.job = job
  5. self.age = age
  6.  
  7. people = [('abc','worker',20),
  8. ('efg', 'student',22),
  9. ('hlk', 'doctor', 25)] # yeah, right!
  10.  
  11. personList = []
  12.  
  13. for name,job,age in people:
  14. personList.append(Person(name,job,age))

Jeff

Hi, Jeff,

That's the nice code. Thanks so much.
But my problem is that I have thousands people of a large list of class objects to assign the values with about 30 attributes in the class. I can not manually do that.
each attributes is in a list. How can I combine 30 attributes into a tuple as you mentioned above so that I loop through thousands of people?

Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,858
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 867
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: assign the values to each members of a large list of class objects

 
0
  #4
Nov 6th, 2007
What does a typical element in your list look like?
If it's a file, what does a typical data line look like?
Last edited by vegaseat; Nov 6th, 2007 at 1:28 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 977
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 273
woooee woooee is offline Offline
Posting Shark

Re: assign the values to each members of a large list of class objects

 
0
  #5
Nov 6th, 2007
How can I combine 30 attributes into a tuple as you mentioned above so that I loop through thousands of people?
You don't have to use a tuple. That was just a convenient container for the example presented above. As vegaseat already stated, a little more info would be helpful. Give a few examples of input and what it should look like on output. It you take this from a file, and that from a list, etc. be sure to mention that. This should be fairly simple to do with python.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC