Tuplelist access?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

Tuplelist access?

 
0
  #1
Oct 19th, 2008
The comments are running willy-nilly, but at least in the pre-view mode it helps to click "Toggle Plain Text".

if wish == 1:                                       #Menu option
        word = input("Enter word: ")
        description = input("Enter description: ")
        print
        print
        tuplelist.append((word,description))          #tuplelist is originally 
                                                                       defined as simply []
        main_dictuples(tuplelist)                          #Returns the function 
                                                                       for the main menu;
                                                                       ideally updating tuplelist 
                                                                       with a new tuple
                                                                       at the last position...

    if wish == 2:                                              #Menu option
        word = input("Enter word to look up: ")    #Shortened version, 
                                                                       but you get the idea
        if word in tuplelist:
            print "Yep"
        else:
            print "Nope"

Now, option 2 returns Nope regardless of wether the word you enter was entered into the list under option 1 before or not.
So what I'm trying to get to grips with is how to make python realise that the word entered is in the list. Or, to put it another way, to use the variable "word" under both 1 and 2 as a dictionary keyword.
Sheesh, I really have a long way to go...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 926
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Tuplelist access?

 
0
  #2
Oct 19th, 2008
when you go.
  1. if word in tuplelist:
  2. #do stuff
That is a problem. The reason is your comparing something that looks like this:
  1. "Hello"
To something that looks like this
  1. [("hello","something you say as a greeting")]
Now those dont look quite the same right?
So what you need to do is say,
  1. if True in [word == f[0] for f in tuplelist]:
  2. #do stuff
That might not be the most efficient way but it will work better.
Last edited by Paul Thompson; Oct 19th, 2008 at 5:12 pm.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

Re: Tuplelist access?

 
0
  #3
Oct 19th, 2008
Thanks for the quick reply, now at least I understand that much more. Just one more of those staring-at-semantics-error-not-getting-what-the-syntax-wants-problems:

As far as I understand, the operation you provided means that the program identifies the tuple we're after and thus, asking it to print f[1] should give the other half of the tuple, in this case the description. But say for example that I first enter ("Hello","Greeting") under option 1 and then also add in ("No","Refusal") and then ask it to look up the word "Hello":
It answers "Refusal".

I thought this setup would create a new tuple for each new entry? Or is it just me misunderstanding what the program does when looking up?
Sorry for the daft questions...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 960
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 220
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Tuplelist access?

 
0
  #4
Oct 19th, 2008
You could use this, if you want to access the tuples
  1. # select the tuples which first element is word
  2. selection = [item for item in tuplelist if item[0] == word]
  3. if selection:
  4. print word, "was found!"
  5. print selection
  6. else:
  7. print word, "was not found"
However, a more efficient way to do this would be to use dictionaries.
Last edited by Gribouillis; Oct 19th, 2008 at 6:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

Re: Tuplelist access?

 
0
  #5
Oct 20th, 2008
It would, this is just a matter of me wanting to understand this completely; the way I see it, you don't get good at things by ignoring the parts you don't grasp immediately.

Still, though, there seems to be things I mess up:

I've experimented quite a bit and I know that the list itself looks like it should. For example I did like this:
word = input("Enter word to look up: ")
        selection = [item for item in tuplelist if item[0] == word]
        if selection:
            print
            print
            print tuplelist 
        else:
            print "Nope"

... and it writes tuplelist exactly the way I want it. But it won't let me indetify the index of a particular tuple.

If I write like this:
word = input("Enter word to look up: ")
        selection = [item for item in tuplelist if item[0] == word]
        if selection:
            print
            print
            print tuplelist
            print
            print
            print item[1] 
        else:
            print "Nope"

... it evidently recognizes the word I entered (if it wasn't in the list it'd return "Nope", which it doesn't), and prints tuplelist looking good with the right word and description paired together. But it interprets item[1] to be the second part of the

last tuple.
Say for example I've made the three entries
"Hello" and "Greeting"
"No" and "Refusal"
"What" and "Wondering"
the snippet above will return

[('Hello', 'Greeting'), ('No', 'Refusal'), ('What', 'Wondering')]


Wondering

even if I told it to look for the word "Hello". Even if, and this is making me really lost in the fog, I do like this (the same entries in the list as before):

word = input("Enter word to look up: ")
        selection = [item for item in tuplelist if item[0] == word]
        if selection:
            a = tuplelist.index(item)
            print
            print
            print tuplelist[a]
        else:
            print "Nope"

it simply returns a variation on the above:
[('Hello', 'Greeting'), ('No', 'Refusal'), ('What', 'Wondering')]


('Va', 'Wondering')


So, well, um... help?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 960
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 220
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Tuplelist access?

 
0
  #6
Oct 20th, 2008
Don't use item after the selection=... . in this expression, item varies from the first element to the last element of the list. You can use selection , which is a list of all the tuples in tuplelist which first element is word. You could obtain a list of indexes instead, like this
  1. indexlist = [ i for i in xrange(len(tuplelist)) if tuplelist[i][0] == word]
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

Re: Tuplelist access?

 
0
  #7
Oct 20th, 2008
Well, I got something working, looks like this:

word = input("Enter word to look up: ")
        selection = [item for item in tuplelist if item[0] == word]
        if selection:
            a = selection[0] 
            print
            print
            print a[1]
            print
            print
        else:
            print "Nope"

Thanks for the input guys, it was very helpful, not to mention useful!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC