Search for item in list?

This is the place for queries that don't fit in any of the other categories.

Search for item in list?

Postby XndrK » Wed Oct 16, 2013 8:19 pm

I'm making a program for Rock Paper Scissors Lizard Spock, and I am making a program that converts the name of a weapon to a number. This is what I have right now.

Code: Select all
def name_to_number(name):
    """Converts name of weapon to computer number"""
    number = None
    name_dict = {
                 "rock" : 0,
                 "Spock" : 1,
                 "paper" : 2,
                 "lizard" : 3,
                 "scissors" : 4
                 }
    if name in name_dict:
        number = name_dict[name]
    else:
        number = None
    return number


What I want to know is, can I search for an item in a list and get its index?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Search for item in list?

Postby Kebap » Wed Oct 16, 2013 9:02 pm

Hey, you are confusing list and dictionary, [] and {} :mrgreen:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Search for item in list?

Postby micseydel » Wed Oct 16, 2013 10:06 pm

You don't need the else branch since you set it to None above. Although I would just get rid of the earlier assignment and then change
Code: Select all
if name in name_dict:
        number = name_dict[name]
    else:
        number = None
    return number

to
Code: Select all
    return name_dict.get(name, None)

To answer your question though, if your names were sorted, you could do a binary search and get the index that way. But then you'd take O(log n) time instead of O(1). Not that it really matters here. But I think that the dictionary is a lot cleaner.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1355
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Search for item in list?

Postby Yoriz » Wed Oct 16, 2013 10:06 pm

You are searching for an item in a dict not a list.
Code: Select all
>>> name_dict = {
                 "rock" : 0,
                 "Spock" : 1,
                 "paper" : 2,
                 "lizard" : 3,
                 "scissors" : 4
                 }
>>> name_dict.get("paper")
2
>>> name_dict.get("Bannana")
>>>

Using get will return the value that is asigned to the key or return None as default(you can change this behavour by passing a second argument to want you want to be returned if no key is found)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 832
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Search for item in list?

Postby micseydel » Wed Oct 16, 2013 10:37 pm

Yoriz wrote:Using get will return the value that is asigned to the key or return None as default

Cool, didn't realize that it returned None by default, thanks!
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1355
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Search for item in list?

Postby XndrK » Wed Oct 16, 2013 10:46 pm

Kebap wrote:Hey, you are confusing list and dictionary, [] and {} :mrgreen:


I was using a dictionary because I didn't know how to make the program run with a list.

The dictionary was a kludge.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Search for item in list?

Postby XndrK » Wed Oct 16, 2013 10:48 pm

Nevermind, solved own problem.

Use list.index(). Silly me.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 6 guests