Grabbing an item from a list of tuples
I am writing a script which returns a list of tuples, like so: [('http://www.news.com/frontdoor/0-1.html?tag=hd_ts', 'CNET'), ('http://www.news.com/shopping/0-1257.html?tag=hd_ts', 'Price comparisons')] My question (and it's a dumb one) is: how do I refer to the individual values within the tuples when iterating through the list? Here's what I've got so far: myCount = 0 for elem in p.anchors: for elem in elem: print myCount, ": ", elem[0:] myCount = myCount + 1 Here's what it returns: 0 : http://www.news.com/frontdoor/0-1.html?tag=hd_ts 1 : CNET 2 : http://www.news.com/shopping/0-1257.html?tag=hd_ts 3 : Price comparisons How can I specifically reference the urls/titles separately? Am I making sense? Thanks, Aaron Gillette abg@comco-inc.com
On Friday 19 Jul 2002 4:33 pm, abg@comco-inc.com wrote:
I am writing a script which returns a list of tuples, like so:
[('http://www.news.com/frontdoor/0-1.html?tag=hd_ts', 'CNET'), ('http://www.news.com/shopping/0-1257.html?tag=hd_ts', 'Price comparisons')]
My question (and it's a dumb one) is: how do I refer to the individual values within the tuples when iterating through the list?
Here's what I've got so far:
myCount = 0
for elem in p.anchors: for elem in elem: print myCount, ": ", elem[0:] myCount = myCount + 1
Here's what it returns:
0 : http://www.news.com/frontdoor/0-1.html?tag=hd_ts 1 : CNET 2 : http://www.news.com/shopping/0-1257.html?tag=hd_ts 3 : Price comparisons
How can I specifically reference the urls/titles separately?
myCount = 0 for url,title in p.anchors: print url, title myCount += 1
Aaron, In this case you might want to consider using a dictionary. Instead of a list of tuples, you would get: {'http://www.news.com/frontdoor/0-1.html?tag=hd_ts': 'CNET', 'http://www.news.com/shopping/0-1257.html?tag=hd_ts': 'Price comparisons'} This would allow you to access each dictionary entry using the URL as the key: elem = pAnchors['http://www.news.com/frontdoor/0-1.html?tag=hd_ts'] # would yield 'CNET' If you desired, you could also have the dictionary value for each key be a tuple. Kevin -----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of abg@comco-inc.com Sent: Friday, July 19, 2002 11:34 AM To: zope@zope.org Subject: [Zope] Grabbing an item from a list of tuples I am writing a script which returns a list of tuples, like so: [('http://www.news.com/frontdoor/0-1.html?tag=hd_ts', 'CNET'), ('http://www.news.com/shopping/0-1257.html?tag=hd_ts', 'Price comparisons')] My question (and it's a dumb one) is: how do I refer to the individual values within the tuples when iterating through the list? Here's what I've got so far: myCount = 0 for elem in p.anchors: for elem in elem: print myCount, ": ", elem[0:] myCount = myCount + 1 Here's what it returns: 0 : http://www.news.com/frontdoor/0-1.html?tag=hd_ts 1 : CNET 2 : http://www.news.com/shopping/0-1257.html?tag=hd_ts 3 : Price comparisons How can I specifically reference the urls/titles separately? Am I making sense? Thanks, Aaron Gillette abg@comco-inc.com _______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
From the looks of things you have a data structure like: [(url, title), (url,title), ...] If so then use Python's nifty unpacking feature: for url, title in p.anchors: ..do something with url and title hth, -Casey On Friday 19 July 2002 11:33 am, abg@comco-inc.com wrote:
I am writing a script which returns a list of tuples, like so:
[('http://www.news.com/frontdoor/0-1.html?tag=hd_ts', 'CNET'), ('http://www.news.com/shopping/0-1257.html?tag=hd_ts', 'Price comparisons')]
My question (and it's a dumb one) is: how do I refer to the individual values within the tuples when iterating through the list?
Here's what I've got so far:
myCount = 0
for elem in p.anchors: for elem in elem: print myCount, ": ", elem[0:] myCount = myCount + 1
Here's what it returns:
0 : http://www.news.com/frontdoor/0-1.html?tag=hd_ts 1 : CNET 2 : http://www.news.com/shopping/0-1257.html?tag=hd_ts 3 : Price comparisons
How can I specifically reference the urls/titles separately?
Am I making sense?
Thanks,
Aaron Gillette abg@comco-inc.com
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
[<abg@comco-inc.com>]
I am writing a script which returns a list of tuples, like so:
[('http://www.news.com/frontdoor/0-1.html?tag=hd_ts', 'CNET'), ('http://www.news.com/shopping/0-1257.html?tag=hd_ts', 'Price comparisons')]
My question (and it's a dumb one) is: how do I refer to the individual values within the tuples when iterating through the list?
Here's what I've got so far:
myCount = 0
for elem in p.anchors: for elem in elem: print myCount, ": ", elem[0:] myCount = myCount + 1
count=0 for tuple in list_of_tuples: print count, tuple[1], ":", tuple[0] # Alternative print statement print "%s %s: %s" % (count,tuple[1],tuple[0]) count=count+1 The syntax elem[0:] picks out all member from 0 to the end of the list or tuple. Also, it is asking for trouble to use the same variable name ("elem") in the inner and outer loops. Cheers, Tom P
participants (5)
-
abg@comco-inc.com -
Casey Duncan -
Kevin Carlson -
Thomas B. Passin -
Toby Dickenson