Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Lists of lists and tuples, and finding things within them

Reply
Thread Tools

Re: Lists of lists and tuples, and finding things within them

 
 
Daniel Nogradi
Guest
Posts: n/a
 
      11-09-2006
> I have a program that keeps some of its data in a list of tuples.
> Sometimes, I want to be able to find that data out of the list. Here is
> the list in question:
>
> [('password01', 'unk'), ('host', 'dragonstone.org'), ('port', '1234'),
> ('character01', 'Thessalus')]
>
> For a regular list, I could do something like x.index('host') and find
> the index of it, but I don't know how to do this for a tuple where the
> data item isn't known in advance. For example, I want to get the "host"
> entry from the list above; but I can only retrieve it if I know what it
> contains (e.g., x.index(('host', 'dragonstone.org'))).
>
> Is there a better way to do this than a construct similar the following?
>
> for key, value in x:
> if key == 'host':
> print value
>


If I were you I would use a dictionary for such a thing:

mydict = dict( password01='unk', host='dragonstone.org', port='1234',
character01='Thessalus' )

And then you would look up host by:

mydict[ 'host' ]

HTH,
Daniel
 
Reply With Quote
 
 
 
 
dakman@gmail.com
Guest
Posts: n/a
 
      11-09-2006
<LameJoke>
Thanks Captain Obvious!
</LameJoke>
Daniel Nogradi wrote:
> > I have a program that keeps some of its data in a list of tuples.
> > Sometimes, I want to be able to find that data out of the list. Here is
> > the list in question:
> >
> > [('password01', 'unk'), ('host', 'dragonstone.org'), ('port', '1234'),
> > ('character01', 'Thessalus')]
> >
> > For a regular list, I could do something like x.index('host') and find
> > the index of it, but I don't know how to do this for a tuple where the
> > data item isn't known in advance. For example, I want to get the "host"
> > entry from the list above; but I can only retrieve it if I know what it
> > contains (e.g., x.index(('host', 'dragonstone.org'))).
> >
> > Is there a better way to do this than a construct similar the following?
> >
> > for key, value in x:
> > if key == 'host':
> > print value
> >

>
> If I were you I would use a dictionary for such a thing:
>
> mydict = dict( password01='unk', host='dragonstone.org', port='1234',
> character01='Thessalus' )
>
> And then you would look up host by:
>
> mydict[ 'host' ]
>
> HTH,
> Daniel


 
Reply With Quote
 
 
 
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      11-09-2006
Daniel Nogradi wrote:
> > I have a program that keeps some of its data in a list of tuples.
> > Sometimes, I want to be able to find that data out of the list. Here is
> > the list in question:
> >
> > [('password01', 'unk'), ('host', 'dragonstone.org'), ('port', '1234'),
> > ('character01', 'Thessalus')]
> >
> > For a regular list, I could do something like x.index('host') and find
> > the index of it, but I don't know how to do this for a tuple where the
> > data item isn't known in advance. For example, I want to get the "host"
> > entry from the list above; but I can only retrieve it if I know what it
> > contains (e.g., x.index(('host', 'dragonstone.org'))).
> >
> > Is there a better way to do this than a construct similar the following?
> >
> > for key, value in x:
> > if key == 'host':
> > print value
> >

>
> If I were you I would use a dictionary for such a thing:
>


(snipped)


The sequence of tuples may have repeated "keys"; if
that's the case, then you may use a filter/iterator instead.
Using a dictionary would otherwise cause you to lose
"values":

import itertools

mytuple = (('foo', 'bar'), ('foo', 'foobar'), ('baz', 'qux'))

it = itertools.ifilter(lambda t : t[0] == 'foo', mytuple)

for t in it:
print t


--
Hope this helps,
Steven

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      11-10-2006
On 9 Nov 2006 12:27:10 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) declaimed the
following in comp.lang.python:

> The sequence of tuples may have repeated "keys"; if
> that's the case, then you may use a filter/iterator instead.
> Using a dictionary would otherwise cause you to lose
> "values":
>

Depends upon how the dictionary is created

> mytuple = (('foo', 'bar'), ('foo', 'foobar'), ('baz', 'qux'))
>

adict = { "foo" : [ "bar", "foobar" ], "baz" : [ "qux"] }
--
Wulfraed Dennis Lee Bieber KD6MOG
(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rar archives and getting things out of them - s/w suggestions wanted. Bruce Sinclair NZ Computing 13 10-19-2010 11:10 PM
Compacting 5,000 Messages, Moving Them To A Google Account, And Burning Them Onto A CD Martin Computer Support 9 01-14-2007 09:33 PM
vs2005 publish website doing bad things, bad things =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?= ASP .Net 1 10-25-2006 06:18 PM
HttpHandlers - Learn Them. Use Them. Anonieko ASP .Net 5 06-16-2006 06:19 PM
Does anyone know them or has any experience with them (good or bad)? Mike Timbell Digital Photography 9 11-13-2003 01:09 AM



Advertisments