简体   繁体   中英

How to use filter() on a string and get multiple word occurences?

I was able to capture the first instance, but trying to get either a count or just duplicate in the returned sets

For example, here is my code simplified,

word_list = ['Geico','Mike']
a_string = "what day is it, Mike Mike Mike"
sets = filter(lambda x:x in a_string,word_list)

... I get ['Mike'] but would like to get ['Mike','Mike','Mike'] ; just some way to better understand how to iterate within filter if possible. I probably do not need each "Mike" for my project, but understanding this will help me wrap my head around the use of filter() with lambda ; so improving this code is more important to me than a new way of doing it.

Understaning Filter:

You are searching otherway around. You should do this filter(needle,haystack) but you are doing filter(haystack,needle)

Example

>>> names = ['Anna', 'Grant', 'Bob', 'David', 'Carrie', 'Barbara', 'Zach']
>>> b_names = filter(lambda s: s.startswith('B'), names)
>>> print b_names
#Output
['Bob', 'Barbara']

filter(function, iterable) is equivalent to [ item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None .

Understanding lamba: lambda is just another word for function When you read the following line, replace lambda by function in your mind:

>>> f = lambda x: x + 10
>>> f(3)
13

Example:2

f = lambda x, y : x * y

The above function is equivalent to the one below.

def f(x, y):
    return x * y

lambda returns a function that given the parameters to the left of the : sign will return the value of the expression on the right of it. More about lambda in this post

from collections import  Counter


word_list = ['Geico','Mike']
a_string = "what day is it, Mike Mike Mike"
li=a_string.split()
print Counter(li)
print [i for i in li if i in word_list]

Output:
Counter({'Mike': 3, 'what': 1, 'it,': 1, 'day': 1, 'is': 1})
['Mike', 'Mike', 'Mike']

Filter is quite intuitive: You give it some kind of iterable and a constraint (your lambda function). It will then go over each item and only keep it, if it fullfills the constraint.

So in your case, you want to iterate the words of your sentence (thus split it first) and only keep them, if they are in your word list:

word_list = ['Geico','Mike']
a_string = "what day is it, Mike Mike Mike"
sets = filter(lambda x: x in word_list, a_string.split())
print(list(sets))

I think you want to filter the list of a_string split by " " by whether each word is in word_list or not.

My answer:

sets = filter(lambda x:x in word_list, a_string.split())

or:

sets = [x for x in a_string.split() if x in word_list]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM