简体   繁体   English

如果满足某些条件,则从包含元组列表的列表中删除元组

[英]Remove tuple from list containing list of tuples if certain condition is met

I have a list containing list of tuples which was obtained after applying postag on word tokenizer. 我有一个包含元组列表的列表,该列表是在词标记器上应用postag之后获得的。 the sample is 样本是

    lis=[[[('This', 'DT') ('PM', 'NNP') ('Doctor', 'NNP'), ('Sambit', 'NNP'), ('Patra', 'NNP'), ('Spokesperson', 'NNP')]],[[('Can', 'MD'), ('Media', 'NNP'), ('lambast', 'VB'), ('Sonia', 'NNP'), ('Gandhi', 'NNP'), ('up', 'RP'), ('Dalit', 'NNP'), ('Sitaram', 'NNP'), ('Dalit', 'NNP'), ('President', 'NNP')]]]

I want to remove the tuples when the second element of the tuple is 'NNP'.The OutputList will look like this; 当元组的第二个元素为'NNP'时,我想删除元组。OutputList将如下所示;

    Final_lis=[[[('This', 'DT')]],[[('Can', 'MD'), ('lambast', 'VB'), ('up', 'RP')]]]

I am writing the code: 我正在编写代码:

   print(len(lis[0][1])) #to print the length of first list containing tuples        
   f_list=[]
   for i in range(0,len(lis)):
       for j in range(len(lis[l])):
           if lis[i][j][1]!='NNP':
              f_list.append(lis[i][j])

But it's showing error 但是显示错误

    Traceback (most recent call last):
    File "<ipython-input-51-02562b867f97>", line 1, in <module>
runfile('C:/Users/meet/t1.py', wdir='C:/Users/meet')

    File "C:\Users\meet\Anaconda3\lib\site- 
   packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
       execfile(filename, namespace)

     File "C:\Users\meet\Anaconda3\lib\site- 
   packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
       exec(compile(f.read(), filename, 'exec'), namespace)

     File "C:/Users/meet/t1.py", line 9, in <module>
       print(len(lis[0][1]))

    IndexError: list index out of range

Try this: 尝试这个:

flist = []
for j in lis:
    for k in j:
        for l in k:
            if l[1] != 'NNP':
                flist.append(l)
print(flist)

This gives you a single list and not a list of lists. 这给您一个列表,而不是列表。 Besides your lis has some issues as there is no , between some tuples. 除了你的lis有一些问题,因为没有,一些元组之间。

The error message says that you are trying to access the second (zero indexed) element of the first list of your object, while this list has only one element. 错误消息表明您正在尝试访问对象的第一个列表的第二个(零索引)元素,而该列表只有一个元素。 Your original code has one loop to few, hence it will always output all the lists due to comparing a list to the string. 您的原始代码只有很少的一个循环,因此由于将列表与字符串进行比较,因此它将始终输出所有列表。

The following code should yield the expected output if you want to keep the previous structure (it may maintain empty lists though): 如果要保留以前的结构,以下代码应产生预期的输出(尽管它可能会保留空列表):

f_list = [
    [
        [
            (val, tag) for val, tag in inner_list if tag != 'NNP'
        ] for inner_list in outer_list
    ]
    for outer_list in lis
]
# result: [[[('This', 'DT')]], [[('Can', 'MD'), ('lambast', 'VB'), ('up', 'RP')]]]

If you only need a list of tuples the following snippet will give the expected outcome: 如果只需要一个元组列表,则以下代码片段将给出预期的结果:

f_list = [
    (val, tag)
    for outer_list in lis
    for inner_list in outer_list
    for val, tag in inner_list
    if tag != 'NNP'
]
# result: [('This', 'DT'), ('Can', 'MD'), ('lambast', 'VB'), ('up', 'RP')]

这将给您您想要的:

result = [[filter(lambda x: x[1] != 'NNP', a[0])] for a in lis]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM