[英]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
!但它为
alii
和liai
返回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.您必须分配给
str1
和str2
结果。 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.