| | |
Tuplelist access?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 13
Reputation:
Solved Threads: 0
The comments are running willy-nilly, but at least in the pre-view mode it helps to click "Toggle Plain Text".
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...
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...
when you go.
That is a problem. The reason is your comparing something that looks like this:
To something that looks like this
Now those dont look quite the same right?
So what you need to do is say,
That might not be the most efficient way but it will work better.
python Syntax (Toggle Plain Text)
if word in tuplelist: #do stuff
python Syntax (Toggle Plain Text)
"Hello"
python Syntax (Toggle Plain Text)
[("hello","something you say as a greeting")]
So what you need to do is say,
python Syntax (Toggle Plain Text)
if True in [word == f[0] for f in tuplelist]: #do stuff
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
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Oct 2008
Posts: 13
Reputation:
Solved Threads: 0
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...
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...
You could use this, if you want to access the tuples
However, a more efficient way to do this would be to use dictionaries.
python Syntax (Toggle Plain Text)
# select the tuples which first element is word selection = [item for item in tuplelist if item[0] == word] if selection: print word, "was found!" print selection else: print word, "was not found"
Last edited by Gribouillis; Oct 19th, 2008 at 6:36 pm.
•
•
Join Date: Oct 2008
Posts: 13
Reputation:
Solved Threads: 0
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:
... 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:
... 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):
it simply returns a variation on the above:
[('Hello', 'Greeting'), ('No', 'Refusal'), ('What', 'Wondering')]
('Va', 'Wondering')
So, well, um... help?
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?
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)
indexlist = [ i for i in xrange(len(tuplelist)) if tuplelist[i][0] == word]
•
•
Join Date: Oct 2008
Posts: 13
Reputation:
Solved Threads: 0
Well, I got something working, looks like this:
Thanks for the input guys, it was very helpful, not to mention useful!
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!
![]() |
Other Threads in the Python Forum
- Previous Thread: Help with writing the output into a file
- Next Thread: Progress Bar
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti apache approximation array backend beginner binary book builtin calculator chmod code converter countpasswordentry curved dan08 dictionaries dictionary drive dynamic examples excel file filename float format function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion redirect refresh scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame write wxpython





