在包含 unicode 字符串的列表上调用 remove() 时,Python 给出 UnicodeWarning

标签 python string list unicode

我有两个包含 unicode 字符串的列表。我正在尝试从 availablesongs 中删除也出现在 recentsongs 中的每个元素。

以下代码导致了问题(为调试而注释掉了异常子句):

for x in recentsongs:
    #try:
        availablesongs.remove(x)
    #except ValueError:
    #   pass

当列表包含纯 ASCII 字符串时,此代码工作正常,但当引入其他语言的字符时,它会失败:

UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  availablesongs.remove(x)
ValueError: list.remove(x): x not in list

错误来自 remove() 函数本身,这让我很困惑。我该如何解决这个问题?

最佳答案

我会做什么:

list(set(availablesongs)-set(recentsongs))

关于在包含 unicode 字符串的列表上调用 remove() 时,Python 给出 UnicodeWarning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10149850/

相关文章:

java - ANTLR 是否提供语义

python - Django 1.8 忽略 DIRS,找不到模板

python - 升级后 Pandas 系列操作非常缓慢

string - 如何在 NSIS 中连接 2 个字符串

c - 如何检查 C 中是否由完全相同的字符组成更多单词?

r - 构造一个命名列表而不必键入每个对象的名称两次

python - 在python中使用正则表达式提取冒号或括号之前的字符串

c - 如何 scanf ("%[^\n]", str);在 C 编程工作?

java - java中的链表

python - Python 中的列表排序(转置)