简体   繁体   English

计算字符串中出现的字符数

[英]count characters occurences in string

I want to find out how often does "reindeer" (in any order) come in a random string and what is the left over string after "reindeer" is removed.我想知道“驯鹿”(以任何顺序)出现在随机字符串中的频率以及删除“驯鹿”后剩下的字符串是什么。 I need to preserve order of the left over string我需要保留剩余字符串的顺序

So for example所以例如

"erindAeer" -> A (reindeer comes 1 time) "erindAeer" -> A (驯鹿来了 1 次)

"ierndeBeCrerindAeer" -> ( 2 reindeers, left over is BCA)

I thought of sorting and removing "reindeer", but i need to preserve the order .我想过排序和删除“驯鹿”,但我需要保留顺序。 What's a good way to do this?有什么好方法可以做到这一点?

Here is the code in Python:这是 Python 中的代码:

def find_reindeers(s):
    rmap = {}
    for x in "reindeer":
        if x not in rmap:
            rmap[x] = 0
        rmap[x] += 1

    hmap = {key: 0 for key in "reindeer"}
    for x in s:
        if x in "reindeer":
            hmap[x] += 1

    total_occ = min([hmap[x]//rmap[x] for x in "reindeer"])

    left_over = ""
    print(hmap, rmap)
    for x in s:
        if (x in "reindeer" and hmap[x] > total_occ * rmap[x]) or (x not in "reindeer"):
            left_over += x

    return total_occ, left_over

print(find_reindeers("ierndeBeCrerindAeer"))

Output for ierndeBeCrerindAeer : ierndeBeCrerindAeer输出:

(2, "BCA")

Here is a rather simple approach using collections.Counter :这是一个使用collections.Counter的相当简单的方法:

from collections import Counter

def purge(pattern, string):
    scount, pcount = Counter(string), Counter(pattern)
    cnt = min(scount[x] // pcount[x] for x in pcount)
    scount.subtract(pattern * cnt)
    return cnt, "".join(scount.subtract(c) or c for c in string if scount[c])

>>> purge("reindeer", "ierndeBeCrerindAeer")
(2, 'BCA')

We can replace those letters after knowing how many times they repeat, and Counter is convenient for counting elements.我们可以在知道它们重复了多少次之后替换这些字母,而Counter可以方便地对元素进行计数。

from collections import Counter

def leftover(letter_set, string):
    lcount, scount = Counter(letter_set), Counter(string)
    repeat = min(scount[l] // lcount[l] for l in lcount)
    for l in lcount:
        string = string.replace(l, "", lcount[l] * repeat)
    return f"{repeat} {letter_set}, left over is {string}"

print(leftover("reindeer", "ierndeBeCrerindAeer"))
print(leftover("reindeer", "ierndeBeCrerindAeere"))
print(leftover("reindeer", "ierndeBeCrerindAee"))

Output:输出:

2 reindeer, left over is BCA
2 reindeer, left over is BCAe
1 reindeer, left over is BCerindAee

You can do it by using count and replace string function:您可以使用计数和替换字符串函数来实现:

import queue
word = "reindeer" 
given_string = "ierndeBeCrerindAeer"
new_string = ""
counter = 0
tmp = ""
letters = queue.Queue()



for i in given_string:
    if not i in word:
        new_string += i
    else:
        letters.put(i)


x = 0
while x < len(word):
    while not letters.empty():
        j = letters.get()
        if j == word[x]:
            tmp += j
            # print(tmp)
            break
        else:
            letters.put(j)
    x = x +1
    if tmp == word:
        counter += 1
        tmp = ""
        x = 0
print(f"The word {word} occurs {counter} times in the string {given_string}.")
print("The left over word is",new_string)

Output will be:输出将是:

The word reindeer occurs 2 times in the string ierndeBeCrerindAeer.
The left over word is BCA

It's easy to use queue here so that we don't repeat the elements that are already present or found.在这里使用队列很容易,这样我们就不会重复已经存在或找到的元素。 Hope this answers your question, Thank you!希望这能回答您的问题,谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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