Guest User

Untitled

a guest
Jan 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. a = [1,2,3,4,5,6,7,8,9,10]
  2. b = [4,5,6]
  3.  
  4. # left sublist
  5. [1,2,3]
  6.  
  7. # right sublist
  8. [7,8,9,10]
  9.  
  10. filter(lambda x: x not in b, a)
  11.  
  12. list(set(a) - set(b))
  13.  
  14. a[:a.index(b[0])]
  15.  
  16. a[a.index(b[-1]) + 1:]
  17.  
  18. def array_split(arr, separator):
  19. result = [] # list of the sublists
  20. current_part = [] # current sublist we're assembling
  21. possible_separator = [] # store possible separator here (e.g. if the separator is 4,5,6 and we've got 4,5 so far)
  22.  
  23. for i in arr:
  24. if i == separator[len(possible_separator)]: # matches next piece of separator?
  25. if len(possible_separator) == len(separator) - 1: # separator complete
  26. result.append(current_part)
  27.  
  28. current_part = []
  29. possible_separator.clear()
  30. else: # add to possible separator
  31. possible_separator.append(i)
  32. else:
  33. current_part.extend(possible_separator) # wasn't the full separator, add to sublist and clear
  34. possible_separator.clear()
  35.  
  36. current_part.append(i)
  37.  
  38. result.append(current_part)
  39. return result
  40.  
  41. 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]]
  42.  
  43. str_a = ",".join(map(lambda x: str(x), a)) # str_a = '1,2,3,4,5,6,7,8,9,10'
  44. str_b = ",".join(map(lambda x: str(x), b)) # str_b = '4,5,6'
  45.  
  46. str_left, str_right = str_a.split(str_b) # str_left = '1,2,3,'
  47. # str_right = ',7,8,9,10'
  48.  
  49. left = list(map(lambda x: int(x), str_left[:-1].split(','))) # [:-1] is there to get rid of the trailing ,
  50. right = list(map(lambda x: int(x), str_right[1:].split(','))) # [1:] is there to get rid of leading ,
  51.  
  52. def get_segment_index(base_list, sub_list):
  53. cursor, len_sub, len_base = 0, len(sub_list), len(base_list)
  54. for i in range(len_base-len_sub+1):
  55. if base_list[i:i+len_sub] == sub_list: # check for the match of sub-list
  56. yield cursor, i
  57. cursor = i+len_sub
  58. else: # to yield the last segment
  59. if cursor != len_base:
  60. yield cursor, len_base
  61.  
  62. >>> a = [1,4,5,6,2,3, 9, 7,8,10,4,5,6,9,2]
  63. >>> b = [4,5,6]
  64.  
  65. >>> [a[x:y] for x, y in get_segment_index(a, b)]
  66. [[1], [2, 3, 9, 7, 8, 10], [9, 2]]
  67.  
  68. >>> a = ['I', 'am', 'sample', 'example', 'to', 'split', 'based', 'on', 'sample', 'example', 'sublist']
  69. >>> b = ['sample', 'example']
  70.  
  71. >>> [a[x:y] for x, y in get_segment_index(a, b)]
  72. [['I', 'am'], ['to', 'split', 'based', 'on'], ['sublist']]
  73.  
  74. >>> a = [1,2,3,4,5,6,7,8,9,10]
  75. >>> b = [4,5,6]
  76.  
  77. >>> left, right = [a[x:y] for x, y in get_segment_index(a, b)]
  78.  
  79. # Left sublist
  80. >>> left
  81. [1, 2, 3]
  82.  
  83. # Right sublist
  84. >>> right
  85. [7, 8, 9, 10]
Add Comment
Please, Sign In to add comment