简体   繁体   English

python:如何检查字符串是否具有相同的字符/重复它们的概率是否相同

[英]python: how to check if string has the same characters / the probability of repeating them is the same

How to check if a given string has the same characters or their probability is the same, which gives me True?如何检查给定的字符串是否具有相同的字符或它们的概率是否相同,这给了我正确的结果?

For example, if there is string = "aaaa" the result is True and:例如,如果有string = "aaaa"结果为 True 并且:

string = "aabb" True
string = "aabbcc" True
p = "1122" True
p = "aaaaBBBB9999$$$$" True

but:但:

string = "korara" False
p = "33211" False

For "aaa" I can use (len (set ("aaa")) == 1) , but I don't know about the others.对于"aaa"我可以使用(len (set ("aaa")) == 1) ,但我不知道其他的。

Have you try ?你试过吗?

from collections import Counter

def check(v):
    return len(set(Counter(v).values())) <= 1

assert check("aabb")
assert check("aabbcc")
assert check("1122")
assert check("aaaaBBBB9999$$$$")
assert check("")

assert not check("korara")
assert not check("33211")

You can use the collection which will create a dictionary and then you can check for each value if they are equal.您可以使用将创建字典的集合,然后您可以检查每个值是否相等。

The following piece of code does that:下面的一段代码就是这样做的:

import collections

counter = dict(collections.Counter("aabbcc"))

expected_value = next(iter(counter.values()))
are_equal = all(value == expected_value for value in counter.values())

print("Result for aabbcc: ", are_equal)

counter = dict(collections.Counter("korara"))

expected_value = next(iter(counter.values()))
are_equal = all(value == expected_value for value in counter.values())

print("Result for korara: ", are_equal)

Pass your string in this function在这个函数中传递你的字符串

def repeating_probability(s):
    sarr={}
    for i in s:
        if i in sarr.keys():
            sarr[i] += 1
        else:
            sarr[i] = 1
    if len(list(set(sarr.values()))) == 1:
        return True
    else:
        return False

不过有一个用于字符串的 length() 函数。

暂无
暂无

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

相关问题 "<i>How can I check if a string has the same characters?<\/i>如何检查字符串是否具有相同的字符?<\/b> <i>Python<\/i> Python<\/b>" - How can I check if a string has the same characters? Python 如何检查样本是否与 Python 中的总体具有相同的概率分布? - How to check if sample has same probability distribution as population in Python? 如何检查2个字符串是否具有相同的字符? - python - how to check if 2 string have same characters or no? - python 如何检查第二个字符串中相应位置的字符是否像第一个字符串一样在相同的 position 中重复? 教育考试 - How to check if the characters in the corresponding positions in the second string are repeating in same position like in first string? Educative EXAM Python编码重复相同的字符串10次,每次计数 - Python coding repeating the same string 10 times with counts on each of them 检查字符串是否具有相同频率的字符,无论是否删除 1 个字符 - check if a string has characters of same frequency with or without the removal of 1 character 如何在字符串中查找相同的字符并将它们显示为数量 - How to find the same characters in a string and display them as a quantity Python 检查字符串是否有字符 - Python check if a string has characters Python:如果该字符串连续不包含7个相同字符的子字符串,则返回True - Python: If the string has no substring of 7 of the same characters in a row, return True 如何检查用户输入是否在字符列表中具有相同的字符? - how to check if user input has same characters in char list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM