| | |
assign the values to each members of a large list of class objects
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
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,065
Reputation:
Solved Threads: 300
•
•
•
•
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
Views: 881 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
anti approximation array avogadro basic beginner builtin cipher clear code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format frange ftp function gui heads homework import input java lapse library line lines linux list lists loop mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pymailer pyqt python random raw_input recursion recursive redirect script scrolledtext server singleton sqlite ssh stamp string strings subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable web-scrape wikipedia windows word wxpython






