| | |
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: 150
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: 1,000
Reputation:
Solved Threads: 283
•
•
•
•
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 ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework ideas import inches input java launcher library line lines linux list lists loop mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






