如何比较python中的两个字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21903842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:50:35  来源:igfitidea点击:

How do I compare two strings in python?

pythonstringpython-2.7string-comparison

提问by user3064366

I have two strings like

我有两个字符串

string1="abc def ghi"

and

string2="def ghi abc"

How to get that this two string are same without breaking the words?

如何在不破坏单词的情况下使这两个字符串相同?

回答by thefourtheye

If you want to know if both the strings are equal, you can simply do

如果你想知道两个字符串是否相等,你可以简单地做

print string1 == string2

But if you want to know if they both have the same set of characters and they occur same number of times, you can use collections.Counter, like this

但是如果你想知道它们是否有相同的字符集并且它们出现的次数相同,你可以使用collections.Counter,像这样

>>> string1, string2 = "abc def ghi", "def ghi abc"
>>> from collections import Counter
>>> Counter(string1) == Counter(string2)
True

回答by philshem

Something like this:

像这样的东西:

if string1 == string2:
    print 'they are the same'

update:if you want to see if each sub-string may exist in the other:

更新:如果您想查看每个子字符串是否可能存在于另一个中:

elem1 = [x for x in string1.split()]
elem2 = [x for x in string2.split()]

for item in elem1:
    if item in elem2:
        print item

回答by Tanveer Alam

>>> s1="abc def ghi"
>>> s2="def ghi abc"
>>> s1 == s2  # For string comparison 
False
>>> sorted(list(s1)) == sorted(list(s2)) # For comparing if they have same characters. 
True
>>> sorted(list(s1))
[' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
>>> sorted(list(s2))
[' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

回答by Nishant Nawarkhede

Try to covert both strings to upper or lower case. Then you can use ==comparison operator.

尝试将两个字符串转换为大写或小写。然后你可以使用==比较运算符。

回答by oxfn

Seems question is not about strings equality, but of setsequality. You can compare them this way onlyby splitting strings and converting them to sets:

似乎问题不是关于字符串相等,而是关于集合相等。您只能通过拆分字符串并将它们转换为集合来以这种方式比较它们:

s1 = 'abc def ghi'
s2 = 'def ghi abc'
set1 = set(s1.split(' '))
set2 = set(s2.split(' '))
print set1 == set2

Result will be

结果将是

True

回答by Anum Sheraz

open both of the files then compare them by splitting its word contents;

打开两个文件,然后通过拆分其单词内容来比较它们;

log_file_A='file_A.txt'

log_file_B='file_B.txt'

read_A=open(log_file_A,'r')
read_A=read_A.read()
print read_A

read_B=open(log_file_B,'r')
read_B=read_B.read()
print read_B

File_A_set = set(read_A.split(' '))
File_A_set = set(read_B.split(' '))
print File_A_set == File_B_set

回答by Archit Kapoor

I am going to provide several solutions and you can choose the one that meets your needs:

我将提供几种解决方案,您可以选择满足您需求的一种:

1) If you are concerned with just the characters, i.e, same characters and having equal frequencies of each in both the strings, then use:

1)如果您只关心字符,即相同的字符并且在两个字符串中每个字符的频率相等,请使用:

''.join(sorted(string1)).strip() == ''.join(sorted(string2)).strip()

2) If you are also concerned with the number of spaces (white space characters) in both strings, then simply use the following snippet:

2)如果您还关心两个字符串中的空格(空白字符)的数量,那么只需使用以下代码片段:

sorted(string1) == sorted(string2)

3) If you are considering words but not their ordering and checking if both the strings have equal frequencies of words, regardless of their order/occurrence, then can use:

3)如果您正在考虑单词而不是它们的排序并检查两个字符串是否具有相同的单词频率,无论它们的顺序/出现如何,那么可以使用:

sorted(string1.split()) == sorted(string2.split())

4) Extending the above, if you are not concerned with the frequency count, but just need to make sure that both the strings contain the same setof words, then you can use the following:

4)扩展上面的,如果你不关心频率计数,而只需要确保两个字符串包含相同的单词,那么你可以使用以下内容:

set(string1.split()) == set(string2.split())

回答by A.G

Equality in direct comparing:

直接比较中的相等性:

string1 = "sample"
string2 = "sample"

if string1 == string2 :
    print("Strings are equal with text : ", string1," & " ,string2)
else :
    print ("Strings are not equal")

Equality in character sets:

字符集中的平等:

string1 = 'abc def ghi'
string2 = 'def ghi abc'

set1 = set(string1.split(' '))
set2 = set(string2.split(' '))

print set1 == set2

if string1 == string2 :
    print("Strings are equal with text : ", string1," & " ,string2)
else :
    print ("Strings are not equal")

回答by Asad

I think difflib is a good library to do this job

我认为 difflib 是一个很好的库来完成这项工作

   >>>import difflib 
   >>> diff = difflib.Differ()
   >>> a='he is going home'
   >>> b='he is goes home'
   >>> list(diff.compare(a,b))
     ['  h', '  e', '   ', '  i', '  s', '   ', '  g', '  o', '+ e', '+ s', '- i', '- n', '- g', '   ', '  h', '  o', '  m', '  e']
    >>> list(diff.compare(a.split(),b.split()))
      ['  he', '  is', '- going', '+ goes', '  home']

回答by Basil Jose

For that, you can use default difflib in python

为此,您可以在 python 中使用默认的 difflib

from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

then call similar() as

然后将 similar() 称为

similar(string1, string2)

it will return compare as ,ratio >= threshold to get match result

它将返回 compare as ,ratio >= threshold 以获得匹配结果