The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
'namedtuple' question
Discuss 'namedtuple' question in the Python Programming forum on Dev Shed. 'namedtuple' question Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 8th, 2012, 09:10 AM
|
 |
Contributing User
|
|
|
|
|
'namedtuple' question
I fooled around with 'namedtuples' and promptly got into troubles:
Code:
from collections import namedtuple
# a test list of (name, age, weight) tuples
data_list = [
('Sarah Gellar', 26, 121),
('Alec Baldwin', 47, 214),
('Mandy Moore', 22, 135),
('Matthew Goode', 29, 167),
('Amanda Bynes', 19, 112),
('James Kirk', 24, 175)
]
# create a list of instances
star_list = [namedtuple('movie_star', 'name age weight') \
for name, age, weight in data_list]
#print(star_list)
print(star_list[1])
print(star_list[1]._fields)
print(star_list[1].name)
print(star_list[2].name)
''' now I am stuck, how to get the name? >>>
<class '__main__.movie_star'>
('name', 'age', 'weight')
<property object at 0x02A8E9C0>
<property object at 0x02A8EBA0>
'''

__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
|

December 8th, 2012, 09:57 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by Dietrich I fooled around with 'namedtuples' and promptly got into troubles: |
Yep. namedtuple() should be used to create a new type, not a new instance.
First create a named tuple with the fields you require, then use your data to occupy a list of these kinds of tuples, as in:
Code:
>>> from collections import namedtuple
>>> Star = namedtuple('Star', ['name', 'age', 'weight'])
>>> data_list = [
('Sarah Gellar', 26, 121),
('Alec Baldwin', 47, 214),
('Mandy Moore', 22, 135),
('Matthew Goode', 29, 167),
('Amanda Bynes', 19, 112),
('James Kirk', 24, 175)
]
>>> movie_stars = [Star(name=name, age=age, weight=weight) for name, age, weight in data_list]
>>>
>>> movie_stars
[Star(name='Sarah Gellar', age=26, weight=121), Star(name='Alec Baldwin', age=47, weight=214), Star(name='Mandy Moore', age=22, weight=135), Star(name='Matthew Goode', age=29, weight=167), Star(name='Amanda Bynes', age=19, weight=112), Star(name='James Kirk', age=24, weight=175)]
>>> movie_stars[1].name
'Alec Baldwin'
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

December 8th, 2012, 10:46 PM
|
 |
Contributing User
|
|
|
|
|
Thanks SuperOscar, you defected my mistake!
Now it works just fine!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|