Suppose I have two arrays:
a = np.array(
[[0, 1],
[2, 3],
[4, 5],
[6, 7]])
b = np.array(
[[2, 3],
[6, 7],
[0, 1],
[4, 5]])
As you can see, one array is simply a shuffle of the other. I need to combine these two arrays to form a third array, c, such as:
- the first part of array
c(until a random indexi) consists of elements from the first part of arraya(until indexi). Therefore,c[:i] == a[:i]must return True. - the rest of the array
cis filled by values from arrayb, that are not already inside arrayc, in the exact same order they appear in.
Given that index i is set to 2, the desired output for above arrays a and b in the code should be:
> c
[[0, 1],
[2, 3],
[6, 7],
[4, 5]]
Array c must be of the same length as both array b and array a, and there is a possibility that two elements within either array a or array b are the same. Array c must also consist of the same elements that are in a and b, (i.e. it behaves somewhat like a shuffle).
I've tried multiple solutions, but none give the desired result. The closest was this:
a = np.arange(10).reshape(5, 2)
np.random.shuffle(a)
b = np.arange(10).reshape(5, 2)
b_part = b[:4]
temp = []
for part in a:
if part in b_part:
continue
else:
temp.append(part)
temp = np.array(temp)
c = copy.deepcopy(np.vstack((b_part, temp)))
However, it sometimes results in array c being smaller than arrays a and b, because the elements in either list can sometimes repeat.
ccan be shorter: Examplea = [(0,1),(2,3),(2,3),(4,5)] b=[(2,3),(4,5),(2,3),(0,1)] i=2So you'd picka[:i]which is[(0,1),(2,3)]and frombwhat has not occurred yet which is(4,5). Thiscwould be[(0,1),(2,3),(4,5)]which is shorter.