![]() |
| ||
| assign the values to each members of a large list of class objects 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 |
| ||
| 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: class person(): Now, if you're willing to modify the __init__ a bit, then you can write even cleaner code: class person(): Jeff |
| ||
| Re: assign the values to each members of a large list of class objects Quote:
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. |
| ||
| 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? |
| ||
| Re: assign the values to each members of a large list of class objects Quote:
|
| All times are GMT -4. The time now is 6:05 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC