匹配Python 3中字符串列表中的重音字符串 [英] Match accentuated strings in lists of string in Python 3

查看:85
本文介绍了匹配Python 3中字符串列表中的重音字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在Python 3中它返回False?并使它返回True的方法是什么?

Why does this return False in Python 3? And what is a way to make it return True?

e = "allé.png"
l = ["allé.png"]

print(e in l)

推荐答案

比较unicode时,您应使用 unicodedata .如果您打算在较大的列表中进行搜索,则可以使用地图或列表理解:

When comparing unicode you should normalize your data using unicodedata. If you intend to search in a large list you could use map or list comprehension:

import unicodedata
from functools import partial

normalize = partial(unicodedata.normalize, 'NFC')

e = "allé.png"
e = normalize(e)
l = ["allé.png"]
print(e in map(normalize, l))

输出

True

或者作为替代:

print(e in [normalize(s) for s in l])

进一步

  1. unicodedata.normalize在python中做什么?
  2. 规范化Unicode
  1. What does unicodedata.normalize do in python?
  2. Normalizing Unicode

这篇关于匹配Python 3中字符串列表中的重音字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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