jstraugh
jstraugh

Reputation: 110

Python: Need help searching through a list for substring

I know this is a simple question but I have a list full of hundreds of strings and I need help trying to select a few of them. For example, the list is:

lines = ["First Sentence dog", "Second Sentence dog", "Third Sentence cat", "Fourth Sentence cat"...]

I need to access and manipulate the indexes that contain the word "dog". The code I have so far is:

    for line in range(len(lines)):
        if "dog" in line:
            # Do some something
        elif "cat" in line:
            # Do some something else
        else:
            # Do other things

Thank you for any help!

Edit: The error I get is TypeError: argument of type 'int' is not iterable

To be specific, my question is: How can I retrieve and do something with the whole string by searching for a specific substring within it?

Upvotes: 0

Views: 1176

Answers (5)

shuttertigress
shuttertigress

Reputation: 41

If you need to do something to the lines, then you could think about gathering all of them into a list first and then applying some function to them. One way could be as follows:

dog_strings = [x for x in lines if 'dog' in x]

Then you can apply a function to all the elements in this list as follows:

edited_dog_strings = map(some_function, dog_strings)

So now your code consists of two lines (and it might be more efficient than a for loop).

Upvotes: 0

Shivam Chauhan
Shivam Chauhan

Reputation: 122

You can try map() function.

First define the function where you can to do something:

def change(string):
     if string = 'dog':
         #do something
     else:
         #do something

Then try map() function. If you want output as list, then:

list(map(change,lines))

Upvotes: 1

Red
Red

Reputation: 27557

You can use enumerate():

for i,line in enumerate(lines):
    if "dog" in line:
        # Do some something
    elif "cat" in line:
        # Do some something else
    else:
        # Do other things

enumerate() will allow you to iterate over an iterable, along with the index of the current element. If you don't need the the index, simply:

for line in lines:
    if "dog" in line:
        # Do some something
    elif "cat" in line:
        # Do some something else
    else:
        # Do other things

Upvotes: 6

Giuseppe
Giuseppe

Reputation: 678

you have to use the index to access the string in the list.

for line in range(len(lines)):
    if "dog" in lines[line]:
        # Do some something
    elif "cat" in lines[line]:
        # Do some something else
    else:
        # Do other things

Upvotes: 1

Vishal Singh
Vishal Singh

Reputation: 6234

lines = [
    "First Sentence dog",
    "Second Sentence dog",
    "Third Sentence cat",
    "Fourth Sentence cat",
]


for index, line in enumerate(lines[:]):
    if "dog" in line:
        lines[index] = "Do Something"
    elif "cat" in line:
        lines[index] = "Do some something else"
    else:
        lines[index] = "Do other things"

print(lines)

Output:

['Do Something', 'Do Something', 'Do some something else', 'Do some something else']

Upvotes: 0

Related Questions