为什么两个相同的字符串不相等? [英] Why are two same strings found to be unequal?

查看:426
本文介绍了为什么两个相同的字符串不相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在列表中搜索字符串。它们看起来完全一样,但是为什么不相等呢?

I'm trying to search for a string within a list. They look exactly the same but why are they not equal?

l = ['liệu',]
a = bytes(l[0],encoding='utf-8')
print(a)

i = 'liệu'
print(bytes(i,encoding='utf-8'))

print(i==l[0])

当前输出:

b'li\xc3\xaa\xcc\xa3u'
b'li\xe1\xbb\x87u'
False

我们如何做到 True

推荐答案

如您所见使用字节表示形式,它们都是不同的字符串:

As you can see with the bytes representation they are both different strings:

>>> a = 'liệu'
>>> b = 'liệu'
>>> a == a
True
>>> b == b
True
>>> a == b
False

在第三个值之后字母:

They differ after the third letter:

>>> for l1, l2 in zip(a, b):
...     print(l1 == l2)
... 
True
True
False
False

这并不意味着 u 不同,但是可以肯定 e 是。

This doesn't mean that the u is different,but for sure the e is.

这篇关于为什么两个相同的字符串不相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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