【问题标题】:How to delete lines from list of tuples?如何从元组列表中删除行?
【发布时间】:2017-05-12 12:14:14
【问题描述】:

我有一个代码可以打印一个带有双元组的列表。进一步来说,我不需要包含某些单词的二元组。我有两个不同的停用词列表,我想将它们应用于二元组。我想将一个列表应用于二元组的第一个单词(索引 [0]),将一个列表应用于第二个单词(索引 [1])。

我尝试过这样的事情:

if gram[0] in stop1 or gram[1] in stop2:
     print(gram)

现在打印了包含停用词的二元组,但是如何从元组中删除这些行?

【问题讨论】:

  • 为什么不直接将filter 与谓词一起使用?

标签: python python-2.7 python-3.x tuples n-gram


【解决方案1】:

如果您不想打印内容存在于停止列表中的元组,请尝试以下操作:

if gram[0] not in stop1 and gram[1] not in stop2:
     print(gram)

【讨论】:

  • @S.H 如果对您有用,请接受答案。它可能会帮助其他人。 :)
【解决方案2】:

使用 for i in range 为您的搜索建立索引...

stopListA = [3,5]
myList = [(1,2), (3,4), (5,6)]
for i in range(len(myList)):
    if myList[i][0] in stopListA:
        myList[i] = myList[i][1]
print(myList)

[(1,2),4,6]

【讨论】:

    猜你喜欢
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 2015-01-23
    • 2017-09-21
    相关资源
    最近更新 更多