Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 8th, 2012, 09:10 AM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 498 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 3 h 4 m 11 sec
Reputation Power: 63
'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

Reply With Quote
  #2  
Old December 8th, 2012, 09:57 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 412 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 7 h 13 m 32 sec
Reputation Power: 65
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)

Reply With Quote
  #3  
Old December 8th, 2012, 10:46 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 498 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 3 h 4 m 11 sec
Reputation Power: 63
Thanks SuperOscar, you defected my mistake!
Now it works just fine!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > 'namedtuple' question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap