| | |
assign the values to each members of a large list of class objects
Thread Solved
![]() |
•
•
Join Date: Oct 2007
Posts: 30
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 149
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:
Now, if you're willing to modify the __init__ a bit, then you can write even cleaner code:
Jeff
My favorite way to do this would be to pull the information out of tuples like this:
Python Syntax (Toggle Plain Text)
class person(): def __init__(self): self.name = ' ' self.job = ' ' self.age = 0 people = [('abc','worker',20), ('efg', 'student',22), ('hlk', 'doctor', 25)] # yeah, right! personList = [] for name,job,age in people: # this unpacks each tuple in the list. tmp = person() tmp.name = name tmp.job = job tmp.age = age 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)
class person(): def __init__(self,name='',job='',age=0): self.name = name self.job = job self.age = age people = [('abc','worker',20), ('efg', 'student',22), ('hlk', 'doctor', 25)] # yeah, right! personList = [] for name,job,age in people: personList.append(Person(name,job,age))
Jeff
•
•
Join Date: Oct 2007
Posts: 30
Reputation:
Solved Threads: 0
•
•
•
•
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)
class person(): def __init__(self): self.name = ' ' self.job = ' ' self.age = 0 people = [('abc','worker',20), ('efg', 'student',22), ('hlk', 'doctor', 25)] # yeah, right! personList = [] for name,job,age in people: # this unpacks each tuple in the list. tmp = person() tmp.name = name tmp.job = job tmp.age = age 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)
class person(): def __init__(self,name='',job='',age=0): self.name = name self.job = job self.age = age people = [('abc','worker',20), ('efg', 'student',22), ('hlk', 'doctor', 25)] # yeah, right! personList = [] for name,job,age in people: 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.
•
•
Join Date: Dec 2006
Posts: 977
Reputation:
Solved Threads: 273
•
•
•
•
How can I combine 30 attributes into a tuple as you mentioned above so that I loop through thousands of people?
![]() |
Similar Threads
- Differences Between Java and C/C++ (C++)
- only const static integral data members can be initialized inside a class or struct (C++)
- Linked List & Objects (C++)
- Array of Class Objects? (C++)
- Passing Class Objects, Instances (C++)
- Instantiating class objects?? (C++)
- Need Big Help !! (Java)
- Help w/ dynamic list funtction definitions (C++)
- Dynamic memory allocation homework (C++)
Other Threads in the Python Forum
- Previous Thread: KeyPress event with holding down the key
- Next Thread: noob: Decimal Comparison in Python
| Thread Tools | Search this Thread |
abrupt accessdenied ansi anti apache application approximation argv array assignment backend beginner binary bluetooth builtin calculator change character converter countpasswordentry curved customdialog dan08 dictionary edit exe file float format function gnu heads homework ideas inches input java keyboard lapse leftmouse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook pointer prime programming py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session software sprite sqlite statictext statistics string strings syntax terminal text thread threading time tlapse tuple twoup ubuntu unicode unit urllib urllib2 variable wordgame write wxpython xlib






