Reputation: 595
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', '.']]
Upvotes: 0
Views: 375
Reputation: 1232
Third element for each sub-list split along the tab character('\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']]
Upvotes: 0
Reputation: 8273
List comprehension split()
and then fetch [2]
element(third element)
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
[[i.split('\t')[-1] for i in subl] for subl in test_list]
Upvotes: 0
Reputation: 126
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)
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.
Upvotes: 1
Reputation: 88236
You could use a list comprehension
and split
each string on \t
and select the third elements. 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é']]
Upvotes: 0