简体   繁体   中英

How to remove element of list based on condition in the sublist

I have the following list of list:

list_of_list = [["-", " ", "P"], ["-", " ", "L"], ["-", " ", "R"], ["K", "|", "K"], ["K", "|", "K"], ["I", " ", "-"], ["K", "|", "K"], ["-", " ", "K"], ["-", " ", "V"], ["-", " ", "D"], ["-", " ", "P"], ["-", " ", "K"], ["-", " ", "K"], ["-", " ", "D"], ["-", " ", "Q"], ["-", " ", "E"], ["A", "|", "A"], ["N", " ", "-"], ["-", " ", "K"], ["-", " ", "E"], ["R", "|", "R"], ["-", " ", "L"], ["-", " ", "K"], ["R", "|", "R"], ["S", " ", "-"], ["N", " ", "-"], ["S", " ", "-"], ["K", "|", "K"], ["-", " ", "I"], ["-", " ", "R"], ["K", "|", "K"], ["-", " ", "L"], ["-", " ", "E"]]

Which looks like this:

In [48]: list_of_list
Out[48]:
[['-', ' ', 'P'], # remove this
 ['-', ' ', 'L'], # remove this
 ['-', ' ', 'R'], # remove this
 ['K', '|', 'K'],
 ['K', '|', 'K'],
 ['I', ' ', '-'],
 ['K', '|', 'K'],
 ['-', ' ', 'K'],
 ['-', ' ', 'V'],
 ['-', ' ', 'D'],
 ['-', ' ', 'P'],
 ['-', ' ', 'K'],
 ['-', ' ', 'K'],
 ['-', ' ', 'D'],
 ['-', ' ', 'Q'],
 ['-', ' ', 'E'],
 ['A', '|', 'A'],
 ['N', ' ', '-'],
 ['-', ' ', 'K'],
 ['-', ' ', 'E'],
 ['R', '|', 'R'],
 ['-', ' ', 'L'],
 ['-', ' ', 'K'],
 ['R', '|', 'R'],
 ['S', ' ', '-'],
 ['N', ' ', '-'],
 ['S', ' ', '-'],
 ['K', '|', 'K'],
 ['-', ' ', 'I'],
 ['-', ' ', 'R'],
 ['K', '|', 'K'],
 ['-', ' ', 'L'],  # remove this
 ['-', ' ', 'E']]  # remove this

As shown in the output above, what I want to do is to remove the element of the beginning and the end of the list. In the beginning, we remove all the lists where the 2nd element is empty until it finds | . In the end, we remove all the list after the last | found is empty.

The desired output is this:

[['K', '|', 'K'],
 ['K', '|', 'K'],
 ['I', ' ', '-'],
 ['K', '|', 'K'],
 ['-', ' ', 'K'],
 ['-', ' ', 'V'],
 ['-', ' ', 'D'],
 ['-', ' ', 'P'],
 ['-', ' ', 'K'],
 ['-', ' ', 'K'],
 ['-', ' ', 'D'],
 ['-', ' ', 'Q'],
 ['-', ' ', 'E'],
 ['A', '|', 'A'],
 ['N', ' ', '-'],
 ['-', ' ', 'K'],
 ['-', ' ', 'E'],
 ['R', '|', 'R'],
 ['-', ' ', 'L'],
 ['-', ' ', 'K'],
 ['R', '|', 'R'],
 ['S', ' ', '-'],
 ['N', ' ', '-'],
 ['S', ' ', '-'],
 ['K', '|', 'K'],
 ['-', ' ', 'I'],
 ['-', ' ', 'R'],
 ['K', '|', 'K']]

How can I achieve that?

Just slicing:

s = ''.join(b for a,b,c in list_of_list)
list_of_list[s.find('|'):s.rfind('|')+1]

Slicing + enumerate :

l=[i for i,v in enumerate(list_of_list) if v[1]=='|']
l2=list_of_list[l[0]:l[-1]+1]
print(l2)

Output:

[['K', '|', 'K'], ['K', '|', 'K'], ['I', ' ', '-'], ['K', '|', 'K'], ['-', ' ', 'K'], ['-', ' ', 'V'], ['-', ' ', 'D'], ['-', ' ', 'P'], ['-', ' ', 'K'], ['-', ' ', 'K'], ['-', ' ', 'D'], ['-', ' ', 'Q'], ['-', ' ', 'E'], ['A', '|', 'A'], ['N', ' ', '-'], ['-', ' ', 'K'], ['-', ' ', 'E'], ['R', '|', 'R'], ['-', ' ', 'L'], ['-', ' ', 'K'], ['R', '|', 'R'], ['S', ' ', '-'], ['N', ' ', '-'], ['S', ' ', '-'], ['K', '|', 'K'], ['-', ' ', 'I'], ['-', ' ', 'R'], ['K', '|', 'K']]

You can use itertools.groupby :

import itertools
def remove_elements(l):
  new_data = [[a, list(b)] for a, b in itertools.groupby(l, key=lambda x:x[1] == ' ')]
  _, *content, _end = new_data
  return [c for _, c in content]

list_of_list = [["-", " ", "P"], ["-", " ", "L"], ["-", " ", "R"], ["K", "|", "K"], ["K", "|", "K"], ["I", " ", "-"], ["K", "|", "K"], ["-", " ", "K"], ["-", " ", "V"], ["-", " ", "D"], ["-", " ", "P"], ["-", " ", "K"], ["-", " ", "K"], ["-", " ", "D"], ["-", " ", "Q"], ["-", " ", "E"], ["A", "|", "A"], ["N", " ", "-"], ["-", " ", "K"], ["-", " ", "E"], ["R", "|", "R"], ["-", " ", "L"], ["-", " ", "K"], ["R", "|", "R"], ["S", " ", "-"], ["N", " ", "-"], ["S", " ", "-"], ["K", "|", "K"], ["-", " ", "I"], ["-", " ", "R"], ["K", "|", "K"], ["-", " ", "L"], ["-", " ", "E"]]
print(remove_elements(list_of_list))

Output:

[[['K', '|', 'K'], ['K', '|', 'K']], [['I', ' ', '-']], [['K', '|', 'K']], [['-', ' ', 'K'], ['-', ' ', 'V'], ['-', ' ', 'D'], ['-', ' ', 'P'], ['-', ' ', 'K'], ['-', ' ', 'K'], ['-', ' ', 'D'], ['-', ' ', 'Q'], ['-', ' ', 'E']], [['A', '|', 'A']], [['N', ' ', '-'], ['-', ' ', 'K'], ['-', ' ', 'E']], [['R', '|', 'R']], [['-', ' ', 'L'], ['-', ' ', 'K']], [['R', '|', 'R']], [['S', ' ', '-'], ['N', ' ', '-'], ['S', ' ', '-']], [['K', '|', 'K']], [['-', ' ', 'I'], ['-', ' ', 'R']], [['K', '|', 'K']]]

Or slightly longer solution, although more robust:

_start = [i for i in range(len(l)) if all(c[1] == ' ' for c in l[:i])]
_end = [i for i in range(len(l)) if all(c[1] == ' ' for c in l[i:])]
new_l = l[0 if not _start else _start[-1]:len(l) if not _end else _end[-1]-1]

Just play with indexes

def ind(l):
    for i,v in enumerate(l):
        if v[1]=="|": return i

ll = ll[ind(ll):]
ll[::-1][ind(ll[::-1]):][::-1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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