简体   繁体   中英

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

I want check if 2 string have same characters or no?

like:
"aand" and "daan" => true
"aafw" and "kaaw" => false

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 !

what I do?

You can iterate over a python-string just like you iterate over a list/tuple. A simple function would be:

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. Now, for checking the number of occurrences, you can use the collections as:

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. So str.replace method returns a new string. You must assign to str1 and str2 result. 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. 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. 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

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