1

Very basic question but is there a way for me to extract a string in a list that contains a word that I want? Something like:

wordNeeded=str(input("blue or red?"))

list1=["A blue car", "A blue bike", "A red bike"]

and then it'll extract the strings which contain the exact word in wordNeeded?

Doobius
  • 127
  • 4

3 Answers3

2

Among other ways, you could use a list comprehension:

list1 = ["A blue car", "A blue bike", "A red bike"]
result = [item for item in list1 if wordNeeded in item]
print(result)
# ["A red bike"]

Alternatively, you could look into filter in combination with a lambda function:

result = filter(lambda x: wordNeeded in x, list1)
print(list(result))

The latter is more complicated in this case but yields the same result.


As for exact words, you either need to split each item before (+eventually lowercase it):
wordNeeded = "blue"
list1 = ["A blue car", "A blue bike", "A red bike", "bluebells are cool."]

result = [item for item in list1
          if any(wordNeeded.lower() == x.lower() for x in item.split())]
print(result)
# ['A blue car', 'A blue bike']

Or use a regular expression with word boundaries altogether:

import re
rx = re.compile(r'\b{}\b'.format(wordNeeded), flags=re.I)
result = [item for item in list1 if rx.search(item)]
print(result)
Jan
  • 42,290
  • 8
  • 54
  • 79
  • 1
    Use `next((s for s in list1 if 'blue' in s), None)` if you want to get just a single item to avoid traversing the whole list. – Eugene Pakhomov May 11 '20 at 11:03
  • @Jan what does the 'item' represent? I've not declared anything else – Doobius May 11 '20 at 11:06
  • @AaryanPatil: The `item` is a loop variable and refers to the actual element when looping over an iterable (in this case a list). – Jan May 11 '20 at 11:07
  • Depending on what's required by "**exact** word" this may not quite what's desired as it'll select any matching substring... eg: "bluebell" – Jon Clements May 11 '20 at 11:07
  • @JonClements: True, one might need a regular expression with word boundaries or split on whitespaces before. – Jan May 11 '20 at 11:08
  • @JonClements: I know. Added an example using `any`. But of cOuRse, you're right about the lower-/uppcase scenario, so I ended up providing a regular expression at last. Probably this has been asked before... ok, no, I'm sure that this has been asked before. – Jan May 11 '20 at 11:15
0

You can use a for loop like this:

for (word in list1):
  if (wordNeeded in item):
      ...

The actual word search is pretty simple and has been discussed plenty of time:

Python - Check If Word Is In A String

https://www.geeksforgeeks.org/python-string-find/

bruhsmh
  • 321
  • 2
  • 4
  • 12
0
def printList(list, word, list_size): 
    map = [0] * NO_OF_CHARS 

    for i in word: 
        map[ord(i)] = 1


    word_size = len(word) 
    for i in list: 
        count = 0
        for j in i: 
            if map[ord(j)]: 
                count+=1

                map[ord(j)] = 0
        if count==word_size: 
            print i 

        # Set the values in map for next item 
        for j in xrange(len(word)): 
            map[ord(word[j])] = 1
printList(list1, wordNeeded, len(list1))