gpt4 book ai didi

python - 将元素保留在另一个列表中包含的元组列表中

转载 作者:行者123 更新时间:2023-11-30 21:57:24 24 4
gpt4 key购买 nike

所以我有这个坏词列表:

badWords = ["nu", "să", "o", "și", "de", "i", "pe", "cu", "ce", "mai", "la", "în", "un", "se", "mi", "eu", "ca",
"că"]

我有这个格式的双重列表:

word_list = [(673, 'de'), (606, 'și'), (518, 'o'), (486, 'să'), (484, 'nu'), (469, 'i'), (348, 'pe'), (309, 'răzvan'), (308, 'cu'), (304, 'ce'), (302, 'mai')]

我尝试这样做,以便删除与我的坏词列表匹配的条目

for x in range(len(word_list)):
for y in range(len(word_list[x])):
for word in badWords:
if(word_list[x][y] == word):
word_list.remove(x)

但我明白了:

Error: list.remove(x): x not in list

最佳答案

您最好使用 list comprehension 创建一个新列表,并仅保留那些在 badWords 中不存在第二项的元组:

[i for i in word_list if i[1] not in badWords]
# [(309, 'răzvan')]

关于python - 将元素保留在另一个列表中包含的元组列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55262776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com