python - Splitting a list with a separator -
I wrote a function that receives two arguments: a list and one Value is present in the given list The objective of the sep function is to split the given list without the specified value in the second argument of written form and return many lists to the list. Therefore def split_list ([1,2,3,2,1], 2) ---> the result will be [[1], [3], [1]]. The spliting functionality is good, but the result holds the second value of the function in another list (SEP). I could think of the way to solve this problem. Thanks in advance
def split_list (l, sep): occurence = [i, enumerate in x (l) if x == sep] newlist = [] newlist.append (l [: Event for [0]]) in range (0, lane (incident)): j = i + 1 if j & lt; << Code>
] in L: LL == SEP: nl.append ([]) and: # Add to last list nl [-1] .append (el) return nlOr with your method, using the list of occurences:
Def split_list (L, sep): # occurences o = [i for i, calculation in n (l) if x == sep] nl = [] # first piece nl.append (l [: o [0]]) The middle slice for # (I, lane) in the # range: nl.append (l [o [i-1] + 1: o [i]] # last piece nl.append (l [o [-1] ] +1:]) back nl
Comments
Post a Comment