user2962806
user2962806

Reputation: 47

Python not reading from list corretly

Okay, below is my issue:

this program reads from a file, makes a list without using rstrip('\n'), which I did on purpose. From there, it prints the list, sorts it, prints it again, saves the new, sorted list to a text file, and allows you to search the list for a value.

The problem I am having is this:

when I search for a name, no matter how I type it in, it tells me that its not in the list.

the code worked til I changed the way I was testing for the variable. Here is the search function:

def searchNames(nameList):
    another = 'y'
    while another.lower() == 'y':
        search = input("What name are you looking for? (Use 'Lastname, Firstname', including comma: ")

        if search in nameList:
            print("The name was found at index", nameList.index(search), "in the list.")
            another = input("Check another name? Y for yes, anything else for no: ")
        else:
            print("The name was not found in the list.")
            another = input("Check another name? Y for yes, anything else for no: ")

For the full code, http://pastebin.com/PMskBtzJ

For the content of the text file: http://pastebin.com/dAhmnXfZ

Ideas? I feel like I should note that I have tried to add ( + '\n') to the search variable

Upvotes: 0

Views: 169

Answers (3)

dstromberg
dstromberg

Reputation: 7177

You've not given us much to go on, but maybe using sys.stdin.readline() instead of input() would help? I don't believe 2.x input() is going to leave a newline on the end of your inputs, which would make the "in" operator never find a match. sys.stdin.readline() does leave the newline at the end.

Also 'string' in list_ is slow compared to 'string' in set_ - if you don't really need indices, you might use a set instead, particularly if your collection is large.

Upvotes: -1

kiasy
kiasy

Reputation: 304

Reason for this error is that any input in your list ends with a "\n". SO for example "john, smith\n". Your search function than uses the input which does NOT include "\n".

Upvotes: 0

abarnert
abarnert

Reputation: 365767

You say you explicitly did not strip off the newlines.

So, your nameList is a list of strings like ['van Rossum, Guido\n', 'Python, Monty\n'].

But your search is the string returned by input, which will not have a newline. So it can't possibly match any of the strings in the list.

There are a few ways to fix this.

First, of course, you could strip the newlines in your list.

Alternatively, you could strip them on the fly during the search:

if search in (name.rstrip() for name in nameList):

Or you could even add them onto the search string:

if search+'\n' in nameList:

If you're doing lots of searches, I would do the stripping just once and keep a list of stripped names around.


As a side note, searching the list to find out if the name is in the list, and then searching it again to find the index, is a little silly. Just search it once:

try:
    i = nameList.index(search)
except ValueError:
    print("The name was not found in the list.")
else:
    print("The name was found at index", i, "in the list.")
another = input("Check another name? Y for yes, anything else for no: ")

Upvotes: 3

Related Questions