Pythonic way of counting entries from a list in a row

What is the best way to find the number of occurrences of rows from a list in a target row? In particular, I have a list:

string_list = [
    "foo",
    "bar",
    "baz"
]

target_string = "foo bar baz bar"

# Trying to write this function!
count = occurrence_counter(target_string) # should return 4

I would like to optimize to minimize speed and memory usage, if that matters. Regarding size, I would expect it string_listto contain several hundred substrings.

+4
source share
8 answers

It works!

def occurrence_counter(target_string):
    return sum(map(lambda x: x in string_list, target_string.split(' ')))

The string is divided into tokens, then each token is converted to 1 if it is in the list, and 0 otherwise. The sum function finally summarizes these values.

EDIT: also:

def occurrence_counter(target_string):
    return len(list(filter(lambda x: x in string_list, target_string.split(' '))))
+3
source

: collectlations.Counter:

from collections import Counter
word_counts = Counter(target_string.split(' '))
total = sum(word_counts.get(w, 0)) for w in string_list)
+4

Python3 :

In [4]: string_list = [
   ...:     "foo",
   ...:     "bar",
   ...:     "baz"
   ...: ]
   ...: 
   ...: set_of_counted_word = set(string_list)
   ...: 
   ...: def occurrence_counter(target_str, words_to_count=set_of_counted_word):
   ...:     return sum(1 for word in target_str.strip().split()
   ...:                if word in words_to_count)
   ...: 
   ...: 
   ...: for target_string in ("foo bar baz bar", " bip foo bap foo dib baz   "):
   ...:     print("Input: %r -> Count: %i" % (target_string, occurrence_counter(target_string)))
   ...: 
   ...: 
Input: 'foo bar baz bar' -> Count: 4
Input: ' bip foo bap foo dib baz   ' -> Count: 3

In [5]:
+2

, :

def occurence_counter(x):
    count = 0
    for y in x:
        count +=1
    return count
+1

:

def occurrence_counter(target_string, string_list):
    target_list = target_string.split(' ')
    return len([w for w in target_list if w in string_list])
+1

sum string.count:

def counter(s, lst)
    return sum(s.count(sub) for sub in lst)

.

+1

Trie, (, (?:ba[rz]|foo)) target_string

import re
from trie import Trie

trie = Trie()

substrings = [
    "foo",
    "bar",
    "baz"
]
for substring in substrings:
    trie.add(substring)
print(trie.pattern())
# (?:ba[rz]|foo)

target_string = "foo bar baz bar"
print(len(re.findall(trie.pattern(), target_string)))
# 4

: trie.py

, target_string substring, . 2 ["foo", "bar", "foobar"] "foobar".

: " Python 3": .

+1

, , :

string_list_B = target_string.split(" ")
commonalities = set(string_list) - (set(string_list) - set(string_list_B))
0

Source: https://habr.com/ru/post/1676842/


All Articles