NIk
NIk

Reputation: 123

beginner looking for advice (Python)

I am an absolutely beginner in Python and trying to create algorithm which is looking a word in a list and if it finds one return an index accordingly. I know that it can be done in “more professional” way, but I am trying to create own code (efficient or not). The problem is the code runs out form range.

Thanks in advance!



lst = ['caT.', 'Cat', '.cat', 'NONE', 'caty']

word_to_find = 'cat'

i = 0
j = 0

while i <= len(lst):
    if j+len(word_to_find) <= len(lst[i]):
        if lst[i][j] == 'C' or 'c':
            if lst[i][j+1] == 'a'or'A':
                if lst[i][j+2] == 't'or'T':
                    print(i)
        j+=1
    i+=1

Upvotes: 0

Views: 106

Answers (5)

Jasar Orion
Jasar Orion

Reputation: 686

you can do it using a function like that :

list_of_strings = ["London", "Paris", "Madrid", "Rome", "Berlin"]
target_string = "MADRID"
target_string_lower = target_string.lower()

is_target_in_list = target_string_lower in (string.lower() for string in list_of_strings)

print(is_target_in_list)

Upvotes: 0

leodsc
leodsc

Reputation: 111

If you want just to correct your code, you can start by using the .lower() method in Python. Using it prevents from your code to get too repetitive (using 'c' or 'C' to get the same string) and from bugs as well. As for the bugs, your code had three of it. The first one, is that if you still don't want to use .lower(), you must write the or statement like this:

example = 's'
if example == 's' or example == 'S':
    print(example)

This is the correct way to use the or statement. The second bug is at your while loop:

while i <= len(lst):
    if j+len(word_to_find) <= len(lst[i]):
        if lst[i][j] == 'C' or 'c':
            if lst[i][j+1] == 'a'or'A':
                if lst[i][j+2] == 't'or'T':
                    print(i)
        j+=1
    i+=1

You must remember that the len() function returns the number of elements inside the list or string. As we start counting the position of values at 0 in a object, the last index will be len(lst)-1. For example:

testList = ['a', 'b', 'c']
len(testList) # returns 3
testList[2] # returns 'c'

As you can see, even if I type testList[2], it returns the last value of the list. Your while try to access the lst[5], since it has the less than or equal sign [while <= len(lst)]. Changing it to the less than (<) sign solves the problem.

The last bug I found is your variable j. In your code, it just get added by 1 everytime the loop starts over. The problem is, your next word may not have that specific letter. For example:

lst = ['lion', 'cat']

In your while loop, Python will return a bug for the second word. Cat doesn't have a fourth letter as lion does. To prevent this, you must set the variable j to 0 everytime the loop starts again. You can do this by adding a j = 0 right after your while statement, as below.

lst = ['caT.', 'Cat', '.cat', 'NONE', 'caty']

word_to_find = 'cat'

i = 0

while i < len(lst):
    j = 0
    if j+len(word_to_find) <= len(lst[i]):
        if lst[i][j].lower() == 'c':
            if lst[i][j+1].lower() == 'a':
                if lst[i][j+2].lower() == 't':
                    print(i)
        j+=1
    i+=1

The code above is the final and working form, it will find any string that starts with 'cat'. There is more efficent ways to code a search algorithm like that, but I think you just want help with the bugs in your code. I hope that this help you. Keep coding!

Upvotes: 1

Cory Kramer
Cory Kramer

Reputation: 117866

A more compact way to do this is using a list comprehension. You can use enumerate to iterate over a sequence by tuples of (index, value). Then you can compare your search word as a substring of each element using in, and you can use .lower for case-insensitive comparison.

>>> [idx for idx, value in enumerate(lst) if word_to_find.lower() in value.lower()]
[0, 1, 2, 4]

Upvotes: 1

DedS3t
DedS3t

Reputation: 203

When you right the while loop make sure that you have while i<= len(lst)-1. Becuase then it would go out of range by one.

Upvotes: 0

bit
bit

Reputation: 509

Try this:

lst = ['caT.', 'Cat', '.cat', 'NONE', 'caty']

word_to_find = 'cat'

i = 0
j = 0

while i <= len(lst) - 1:
    if j+len(word_to_find) <= len(lst[i]):
        if lst[i][j] == 'C' or 'c':
            if lst[i][j+1] == 'a'or'A':
                if lst[i][j+2] == 't'or'T':
                    print(i)
        j+=1
    i+=1

Upvotes: -1

Related Questions