为什么我切出卡片套装表情符号后字符串不相等?

发布于 2025-01-21 02:17:38 字数 348 浏览 1 评论 0原文

Python 3.9

cards = ['♥️A','♠️2','♣️3']
ref_list = ['A','2','3']

for a in cards:
    print(a[1:])
    print(a[1:] in ref_list)

输出是

A
False
️2
False
️3
False

如何修改代码以使其正常工作? (使输出true?)

Python 3.9

cards = ['♥️A','♠️2','♣️3']
ref_list = ['A','2','3']

for a in cards:
    print(a[1:])
    print(a[1:] in ref_list)

the output is

A
False
️2
False
️3
False

How should I revise the code to make it work? (Make the output True?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2025-01-28 02:17:38

欢迎来到 Unicode 的世界!

>>> len('♥️A')
3

>>> [hex(ord(c)) for c in '♥️A']
['0x2665', '0xfe0f', '0x41']

>>> '♥️A'[0:1]
'♥'

>>> '♥️A'[0:2]
'♥️'

此字符串中的红心由 黑心套装变体选择器代码点,因此您的完整字符串有 3 个代码点。

考虑不使用字符串连接来表示此数据,并定义一个数据类或至少一个像 <代码>('♥️', 'A') 代替。

Welcome to the world of Unicode!

>>> len('♥️A')
3

>>> [hex(ord(c)) for c in '♥️A']
['0x2665', '0xfe0f', '0x41']

>>> '♥️A'[0:1]
'♥'

>>> '♥️A'[0:2]
'♥️'

The red heart in this string is composed of the Black Heart Suit and the Variation Selector code points, so your complete string has 3 code points.

Consider not using string concatenation to represent this data, and define a dataclass or at least a tuple like ('♥️', 'A') instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文