简体   繁体   English

如何从另一个子列表列表的特定元素创建新的子列表列表

[英]How to create new list of sublists from specific elements of another list of sublists

I have a list of sublists. 我有一个子列表。 Each sublist is a sentence with string. 每个子列表都是带字符串的句子。 I want to remove the third element of each list to create a new list from each sublists. 我想删除每个列表的第三个元素,以便从每个子列表创建一个新列表。 My code is like this : 我的代码是这样的:

I changed my code (update version): 我更改了代码(更新版本):

for list in list_Pos:
    liste_globale = []
    nouvelle_liste = [] 
    for elt in list:
    first, second, third = elt.split()
    #print(first)
    #print(second)
        #print(third)       
    nouvelle_liste.append(third)
        liste_globale.append(nouvelle_liste)

print(liste_globale)

Update : My list of sublist is like this : 更新:我的子列表列表是这样的:

      [ ['Moi\tPRO:PER\tmoi', 'je\tPRO:PER\tje', 'ne\tADV\tne', 'trouve\tVER:pres\ttrouver', 'pas\tADV\tpas', 'très\tADV\ttrès', 'esthétique\tADJ\testhétique', '.\tSENT\t.'], ['L’esthétique\tADJ\tL’esthétique', 'pêche\tNOM\tpêche', 'un\tDET:ART\tun', 'peu\tADV\tpeu', '.\tSENT\t.'], ['Cette\tPRO:DEM\tce', 'grosse\tADJ\tgros', 'prise\tNOM\tprise', 'là\tADV\tlà', '...\tPUN\t...'], ['Cette\tPRO:DEM\tce', 'prise\tNOM\tprise', 'puis\tADV\tpuis', 'la\tDET:ART\tle', 'borne\tNOM\tborne', ',\tPUN\t,', 'ça\tPRO:DEM\tcela', 'se\tPRO:PER\tse', 'voit\tVER:pres\tvoir', '.\tSENT\t.'], ['Derrière\tPRP\tderrière', 'la\tDET:ART\tle', 'télé\tNOM\ttélé', 'ça\tPRO:DEM\tcela', 'va\tVER:pres\taller', 'mais\tKON\tmais', '...\tPUN\t...'], ['Mais\tKON\tmais', 'vu\tVER:pper\tvoir', 'le\tDET:ART\tle', 'système\tNOM\tsystème', 'ça\tPRO:DEM\tcela', 'va\tVER:pres\taller', 'être\tVER:infi\têtre', 'difficile\tADJ\tdifficile', 'de\tPRP\tde', 'faire\tVER:infi\tfaire', 'plus\tADV\tplus', 'sobre\tADJ\tsobre', '!\tSENT\t!'], ['M\tNOM\tM', '.\tSENT\t.', 'Laudrel\tNAM\tLaudrel', 'est\tVER:pres\têtre', 'mort\tVER:pper\tmourir', 'hier\tADV\thier', 'soir\tNOM\tsoir', '.\tSENT\t.'], ['je\tPRO:PER\tje', 'viens\tVER:pres\tvenir', '2.2\tNUM\t@card@', ',\tPUN\t,', 'lo\tVER:pper\tlo', '.\tSENT\t.']]

My output is like this : 我的输出是这样的:

[['je', 'venir', '@card@', ',', 'lo', '.'], ['je', 'venir', '@card@', ',', 'lo', '.'], ['je', 'venir', '@card@', ',', 'lo', '.'], ['je', 'venir', '@card@', ',', 'lo', '.'], ['je', 'venir', '@card@', ',', 'lo', '.'], ['je', 'venir', '@card@', ',', 'lo', '.']]

Based on original edit output, you could use list comprehension for each element of each sublist in the range of length of sublist 1 (assuming all sublists are of same length) 根据原始编辑输出,可以在子列表1的长度范围内对每个子列表的每个元素使用列表理解(假设所有子列表的长度相同)

lst = [['M\tNOM\tM', '.\tSENT\t.', 'Laudrel\tNAM\tLaudrel',],['Derrière\tPRP\tderrière','la\tDET:ART\tle',  'télé\tNOM\tt',]]
final_result = []
for x in range(len(lst[1])):
    lst1 = [[item.split('\t')[x] for item in sblst] for sblst in lst]
    final_result.append(lst1)
print(final_result)

To get only third element, remove for loop and run lst1 = [[item.split('\\t')[2] for item in sblst] for sblst in lst] , where x is just set to 2. 要仅获取第三个元素,请删除for循环,然后为lst1 = [[item.split('\\t')[2] for item in sblst] for sblst in lst]运行lst1 = [[item.split('\\t')[2] for item in sblst] for sblst in lst] ,其中x仅设置为2。

You could use a list comprehension and split each string on \\t and select the third elements. 您可以使用list comprehension并在\\tsplit每个字符串,然后选择第三个元素。 Using the first examples from your list: 使用列表中的第一个示例:

l=[['M\tNOM\tM', '.\tSENT\t.', 'Laudrel\tNAM\tLaudrel'],['Derrière\tPRP\tderrière','la\tDET:ART\tle',  'télé\tNOM\tt']]

You could do: 您可以这样做:

[[i.split('\t')[2] for i in subl] for subl in l]
[['M', '.', 'Laudrel'], ['Derrière', 'la', 'télé']]

Third element for each sub-list split along the tab character('\\t'). 每个子列表的第三个元素都沿制表符('\\ t')分割。

The following solution matches your output sample: 以下解决方案与您的输出样本匹配:

# -*- coding: utf-8 -*-

list_Pos = [
    ['M\tNOM\tM', '.\tSENT\t.', 'Laudrel\tNAM\tLaudrel'],
    ['Derrière\tPRP\tderrière','la\tDET:ART\tle',  'télé\tNOM\tt']
]


final_result = []

for sub_list in list_Pos:
    final_result.append([r.split("\t")[2] for r in sub_list])

print(final_result)

Output: 输出:

[['M', '.', 'Laudrel'], ['derrière', 'le', 't']] [['M','。','Laudrel'],['derrière','le','t']]

List comprehension split() and then fetch [2] element(third element) 列出理解split() ,然后获取[2]元素(第三个元素)

test_list=[['M\tNOM\tM', '.\tSENT\t.', 'Laudrel\tNAM\tLaudrel'],['Derrière\tPRP\tderrière','la\tDET:ART\tle',  'télé\tNOM\tt']]

[[i.split('\t')[2] for i in subl] for subl in test_list]

Output 产量

[['M', '.', 'Laudrel'], ['derrière', 'le', 't']]

Also, if you always need last index use -1 as in index 另外,如果您始终需要最后一个索引,请在索引中使用-1

[[i.split('\t')[-1] for i in subl] for subl in test_list]

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

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