944,180 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1114
  • Python RSS
Nov 5th, 2007
0

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

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
jliu66 is offline Offline
30 posts
since Oct 2007
Nov 5th, 2007
0

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

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:

Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Nov 6th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by jrcagle ...
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:

Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
jliu66 is offline Offline
30 posts
since Oct 2007
Nov 6th, 2007
0

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

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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 6th, 2007
0

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

Quote ...
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.
Reputation Points: 741
Solved Threads: 694
Nearly a Posting Maven
woooee is offline Offline
2,314 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: KeyPress event with holding down the key
Next Thread in Python Forum Timeline: noob: Decimal Comparison in Python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC