Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a = [1,2,3,4,5,6,7,8,9,10]
- b = [4,5,6]
- # left sublist
- [1,2,3]
- # right sublist
- [7,8,9,10]
- filter(lambda x: x not in b, a)
- list(set(a) - set(b))
- a[:a.index(b[0])]
- a[a.index(b[-1]) + 1:]
- def array_split(arr, separator):
- result = [] # list of the sublists
- current_part = [] # current sublist we're assembling
- possible_separator = [] # store possible separator here (e.g. if the separator is 4,5,6 and we've got 4,5 so far)
- for i in arr:
- if i == separator[len(possible_separator)]: # matches next piece of separator?
- if len(possible_separator) == len(separator) - 1: # separator complete
- result.append(current_part)
- current_part = []
- possible_separator.clear()
- else: # add to possible separator
- possible_separator.append(i)
- else:
- current_part.extend(possible_separator) # wasn't the full separator, add to sublist and clear
- possible_separator.clear()
- current_part.append(i)
- result.append(current_part)
- return result
- array_split([1,2,3,4,5,4,5,6,7,8,9,10], [4,5,6]) == [[1, 2, 3, 4, 5], [7, 8, 9, 10]]
- str_a = ",".join(map(lambda x: str(x), a)) # str_a = '1,2,3,4,5,6,7,8,9,10'
- str_b = ",".join(map(lambda x: str(x), b)) # str_b = '4,5,6'
- str_left, str_right = str_a.split(str_b) # str_left = '1,2,3,'
- # str_right = ',7,8,9,10'
- left = list(map(lambda x: int(x), str_left[:-1].split(','))) # [:-1] is there to get rid of the trailing ,
- right = list(map(lambda x: int(x), str_right[1:].split(','))) # [1:] is there to get rid of leading ,
- def get_segment_index(base_list, sub_list):
- cursor, len_sub, len_base = 0, len(sub_list), len(base_list)
- for i in range(len_base-len_sub+1):
- if base_list[i:i+len_sub] == sub_list:
- yield cursor, i
- cursor = i+len_sub
- else:
- if cursor != len_base:
- yield cursor, len_base
- >>> a = [1,4,5,6,2,3, 9, 7,8,10,4,5,6,9,2]
- >>> b = [4,5,6]
- >>> [a[x:y] for x, y in get_segment_index(a, b)]
- [[1], [2, 3, 9, 7, 8, 10], [9, 2]]
- >>> a = [1,2,3,4,5,6,7,8,9,10]
- >>> b = [4,5,6]
- >>> left, right = [a[x:y] for x, y in get_segment_index(a, b)]
- # Left sublist
- >>> left
- [1, 2, 3]
- # Right sublist
- >>> right
- [7, 8, 9, 10]
Add Comment
Please, Sign In to add comment