[英]How to get list members using index from a comprehension
我有 3 個列表。 垃圾郵件列表、詩歌和作者。 我正在嘗試刪除包含任何垃圾詞的詩歌和詩歌作者。 我也試圖與作者一起刪除重復的詩歌。
我已經能夠刪除包含垃圾郵件和重復詩歌的詩歌。 但我無法刪除作者,因此我可以獲得沒有垃圾郵件和重復的詩歌列表以及這些詩歌的作者列表。
poetry = ["we are different", "we are different", "we are not different", "This is a dummy poetry", "This is not art"]
spam_list = ["art", "poetry"]
authors = ["james", "peter", "paul", "felix", "chris"]
# Removes any poetry that contains spam.
h = [sentence for sentence in poetry if not any(spam in sentence.split(' ') for spam in spam_list)]
# Removes duplicates
res = []
for i in h:
if i not in res:
res.append(i)
for lis in res:
print(lis)
詩歌的預期 Output 是:我們不同,我們不不同 作者的預期 Output:詹姆斯,保羅
我需要詩歌 output 的作者。
一個例子:
poetry = ["we are different", "we are different", "we are not different", "This is a dummy poetry", "This is not art"]
authors = ["james", "peter", "paul", "felix", "chris"]
spam_list = ["art", "poetry"]
results = []
processed = []
for author, poetry in zip(authors, poetry):
if poetry not in processed and not any(spam in poetry for spam in spam_list):
processed.append(poetry)
results.append((author, poetry))
print(results)
[('詹姆斯', '我們不一樣'), ('保羅', '我們不一樣')]
一個潛在的問題是結果取決於輸入順序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.