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
?)
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
?)
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.