繁体   English   中英

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

[英]How do I compare two strings in python?

我有两个字符串

string1="abc def ghi"

string2="def ghi abc"

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

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

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

结果将是

True

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

print string1 == string2

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

>>> string1, string2 = "abc def ghi", "def ghi abc"
>>> from collections import Counter
>>> Counter(string1) == Counter(string2)
True
>>> 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']

直接比较中的相等性:

string1 = "sample"
string2 = "sample"

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

字符集中的平等:

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")

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

from difflib import SequenceMatcher

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

然后将 similar() 称为

similar(string1, string2)

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

像这样的东西:

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

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

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

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

如果您只需要检查两个字符串是否完全相同,

text1 = 'apple'

text2 = 'apple'

text1 == text2

结果将是

True

如果您需要匹配的百分比,

import difflib

text1 = 'Since 1958.'

text2 = 'Since 1958'

output = str(int(difflib.SequenceMatcher(None, text1, text2).ratio()*100))

匹配的百分比输出将是,

'95'

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

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

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

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

sorted(string1) == sorted(string2)

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

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

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

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

我认为 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']

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

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

如果你想要一个非常简单的答案:

s_1 = "abc def ghi"
s_2 = "def ghi abc"
flag = 0
for i in s_1:
    if i not in s_2:
        flag = 1
if flag == 0:
    print("a == b")
else:
    print("a != b")

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

这是一个非常基本的例子,但在逻辑比较 (==) 或string1.lower() == string2.lower() ,也许可以用于尝试两个字符串之间距离的一些基本度量。

您可以在任何地方找到与这些或其他一些指标相关的示例,也可以尝试使用 Fuzzywuzzy 包( https://github.com/seatgeek/fuzzywuzzy )。

import Levenshtein
import difflib

print(Levenshtein.ratio('String1', 'String2'))
print(difflib.SequenceMatcher(None, 'String1', 'String2').ratio())

您可以使用简单的循环来检查两个字符串是否相等。 .但理想情况下,您可以使用类似 return s1==s2

s1 = 'hello'
s2 = 'hello'

a = []
for ele in s1:
    a.append(ele)
for i in range(len(s2)):
    if a[i]==s2[i]:
        a.pop()
if len(a)>0:
    return False
else:
    return True

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM