简体   繁体   English

如何检查2个字符串是否具有相同的字符? - python

[英]how to check if 2 string have same characters or no? - python

I want check if 2 string have same characters or no?我想检查 2 个字符串是否有相同的字符?

like:喜欢:
"aand" and "daan" => true "aand" 和 "daan" => true
"aafw" and "kaaw" => false “aafw”和“kaaw”=>假

this is my code:这是我的代码:

def procedures(txt1, txt2):
    str1 = txt1.lower()
    str2 = txt2.lower()
    for i in str1:
        for j in str2:
            if i == j:
                str1.replace(i, "", 1)
                str2.replace(i, "", 1)
                print("did")
    if str1 == "" and str2 == "":
        return True
    else:
        return False

but it returns False for alii and liai !但它为aliiliai返回False

what I do?我所做的?

You can iterate over a python-string just like you iterate over a list/tuple.您可以像遍历列表/元组一样遍历 python 字符串。 A simple function would be:一个简单的 function 将是:

def stringCompare(a, b):
    for i in a:
        if i not in b:
            return False
        
    return True

print(stringCompare("aand", "daan"))
>> True

print(stringCompare("aafw", "kaaw"))
>> False

print(stringCompare("alii", "liai"))
>> True

Note the above function only checks if all the characters in both the string are equal or not.注意上面的 function 只检查两个字符串中的所有字符是否相等。 Now, for checking the number of occurrences, you can use the collections as:现在,为了检查出现次数,您可以使用collections作为:

from collections import Counter

def stringCompare2(a, b):
    # also compares the occurance
    occurance_dict_a = Counter(a)
    occurance_dict_b = Counter(b)
    
    return occurance_dict_a == occurance_dict_b

print(stringCompare2("abc", "aabc"))
>> False

print(stringCompare2("abc", "cba"))
>> True

This is a well-known problem with many possible solutions.这是一个众所周知的问题,有许多可能的解决方案。 Try this:尝试这个:

def procedures(txt1, txt2):
    return sorted(txt1) == sorted(txt2)

Str objects are not editable. Str 对象不可编辑。 So str.replace method returns a new string.所以str.replace方法返回一个新字符串。 You must assign to str1 and str2 result.您必须分配给str1str2结果。 So your code now must be like this:所以你的代码现在必须是这样的:

def procedures(txt1, txt2):
    str1 = txt1.lower()
    str2 = txt2.lower()
    for i in str1:
        for j in str2:
            if i == j:
                str1 = str1.replace(i, "", 1)
                str2 = str2.replace(j, "", 1)
                print("did")
    if str1 == "" and str2 == "":
        return True
    else:
        return False

Or you can make list of characters from original strings and sort them.或者您可以从原始字符串中列出字符并对其进行排序。 So if the strings have same characters, they will be turned into same character sequences (lists).因此,如果字符串具有相同的字符,它们将被转换为相同的字符序列(列表)。

def procedures(txt1, txt2):
    seq1 = list(txt1.lower())
    seq2 = list(txt2.lower())

    seq1.sort()
    seq2.sort()

    if seq1 == seq2:
        return True
    else:
        return False

Do you take the number of occurences into account?您是否考虑了出现次数? If so, one could imagine to turn each str into a list of chars, and then to sort each list.如果是这样,可以想象将每个 str 变成一个字符列表,然后对每个列表进行排序。 It would then remain to compare these lists.然后将继续比较这些列表。

def procedures(txt1, txt2):
str1 = txt1.lower()
str2 = txt2.lower()

str1 = list(str1)
result = False

for dt in str1:
    if dt in str2:
        result = True
    else:
        result = False

return result 

print(procedures('neeraj','prakash'))
print(procedures('neeraj','neeraj'))

you have to create a list for the first text then check one by one in the second str using the loop.您必须为第一个文本创建一个列表,然后使用循环在第二个 str 中逐个检查。 That's it.而已。

first of all we sort both strings.首先,我们对两个字符串进行排序。 second compare them.第二比较它们。

def equal(a,b):
    a = sorted(a)
    b = sorted(b)
    if a == b:
        return True
    return False

声明:本站的技术帖子网页,遵循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 this or that characters in string in python? 如何在Python中检查两个文件(字符串和文件)是否具有相同的内容? - How in Python check if two files ( String and file ) have same content? python:如何检查字符串是否具有相同的字符/重复它们的概率是否相同 - python: how to check if string has the same characters / the probability of repeating them is the same 如何循环字符串中的字符并检查它是否在字典python中 - How to loop characters in string and check if it is in dictionary python 如何让字符串函数检查单个字符而不是整个字符串? - How to have string functions check individual characters instead of whole strings? 如何检查字符串中是否有字符? - How to check if there are characters in a string? 检查两个字符串具有相同数量的字符和相同的字符出现 Python - check two strings have the same numbers of characters and the same character occurrences Python Python 检查字符串是否有字符 - Python check if a string has characters 在Python中检查字符串是否存在字符 - Check String for / against Characters in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM