如何比较python中的两个字符串? [英] How do I compare two strings in python?

查看:114
本文介绍了如何比较python中的两个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串

string1="abc def ghi"

string2="def ghi abc"

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

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

推荐答案

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

Seems question is not about strings equality, but of sets equality. You can compare them this way only by 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

结果是

True

这篇关于如何比较python中的两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆