2017-09-28 4 views
1

Ich habe die folgende Liste von Listen, in denen jede Liste besteht aus 9 Elementen.Entfernen Sie das letzte Element der letzten zwei Listen einer Liste von Listen

ws = [['1.45', '1.04', '1.13', '2.01', '1.46', '1.22', '1.30', '2.60', '2.19'], ['1.71', '1.13', '1.21', '2.07', '1.53', '1.27', '1.47', '2.82', '2.43'], ['1.36', '0.99', '1.03', '1.93', '1.39', '1.14', '1.23', '2.45', '2.06'], ['1.88', '3.24', '1.97', '1.38', '1.67', '3.22', '2.02', '1.57', '1.86'], ['1.95', '3.32', '2.03', '1.44', '1.71', '3.43', '2.14', '1.64', '1.93'], ['1.82', '3.12', '1.88', '1.34', '1.59', '3.14', '1.94', '1.50', '1.80']] 

Ich möchte das letzte Element der letzten 2 Listen entfernen und erhalten:

[['1.45', '1.04', '1.13', '2.01', '1.46', '1.22', '1.30', '2.60', '2.19'], ['1.71', '1.13', '1.21', '2.07', '1.53', '1.27', '1.47', '2.82', '2.43'], ['1.36', '0.99', '1.03', '1.93', '1.39', '1.14', '1.23', '2.45', '2.06'], ['1.88', '3.24', '1.97', '1.38', '1.67', '3.22', '2.02', '1.57', '1.86'], ['1.95', '3.32', '2.03', '1.44', '1.71', '3.43', '2.14', '1.64'], ['1.82', '3.12', '1.88', '1.34', '1.59', '3.14', '1.94', '1.50']] 

Ich habe versucht:

ws_sliced = [l[0:8] for l in ws[-2]] 

Aber das hält eigentlich die letzten 2 Listen (mit 8 Elemente)

I geprüft:

Explain slice notation und https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html

Aber konnte keine Lösung finden.

Antwort

0

ws[-2] nicht schneiden, es ist negativ Indizierung, und es ist die nächste zu letzten Unterliste in Ihrer ws Liste, hier ist die Liste Slicing:

ws = [['1.45', '1.04', '1.13', '2.01', '1.46', '1.22', '1.30', '2.60', '2.19'], 
     ['1.71', '1.13', '1.21', '2.07', '1.53', '1.27', '1.47', '2.82', '2.43'], 
     ['1.36', '0.99', '1.03', '1.93', '1.39', '1.14', '1.23', '2.45', '2.06'], 
     ['1.88', '3.24', '1.97', '1.38', '1.67', '3.22', '2.02', '1.57', '1.86'], 
     ['1.95', '3.32', '2.03', '1.44', '1.71', '3.43', '2.14', '1.64', '1.93'], 
     ['1.82', '3.12', '1.88', '1.34', '1.59', '3.14', '1.94', '1.50', '1.80']] 

# ws[:-2] => Slice ws from the first sub-list to the one before the next-to-last 
# [ws[-2], ws[-1]] => The last and next-to-last sub-lists 
ws_sliced = ws[:-2] + [l[0:8] for l in [ws[-2], ws[-1]]] 
print ws_sliced 

Ausgang:

[['1.45', '1.04', '1.13', '2.01', '1.46', '1.22', '1.30', '2.60', '2.19'], 
['1.71', '1.13', '1.21', '2.07', '1.53', '1.27', '1.47', '2.82', '2.43'], 
['1.36', '0.99', '1.03', '1.93', '1.39', '1.14', '1.23', '2.45', '2.06'], 
['1.88', '3.24', '1.97', '1.38', '1.67', '3.22', '2.02', '1.57', '1.86'], 
['1.95', '3.32', '2.03', '1.44', '1.71', '3.43', '2.14', '1.64'], 
['1.82', '3.12', '1.88', '1.34', '1.59', '3.14', '1.94', '1.50']] 
Verwandte Themen