943,610 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 546
  • Python RSS
Oct 19th, 2008
0

Tuplelist access?

Expand Post »
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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008
Oct 19th, 2008
0

Re: Tuplelist access?

when you go.
python Syntax (Toggle Plain Text)
  1. if word in tuplelist:
  2. #do stuff
That is a problem. The reason is your comparing something that looks like this:
python Syntax (Toggle Plain Text)
  1. "Hello"
To something that looks like this
python Syntax (Toggle Plain Text)
  1. [("hello","something you say as a greeting")]
Now those dont look quite the same right?
So what you need to do is say,
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Oct 19th, 2008
0

Re: Tuplelist access?

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008
Oct 19th, 2008
0

Re: Tuplelist access?

You could use this, if you want to access the tuples
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 930
Solved Threads: 666
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Oct 20th, 2008
0

Re: Tuplelist access?

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008
Oct 20th, 2008
0

Re: Tuplelist access?

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
python Syntax (Toggle Plain Text)
  1. indexlist = [ i for i in xrange(len(tuplelist)) if tuplelist[i][0] == word]
Reputation Points: 930
Solved Threads: 666
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Oct 20th, 2008
0

Re: Tuplelist access?

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Help with writing the output into a file
Next Thread in Python Forum Timeline: Progress Bar





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC