How to get remove the 1st duplicate value of a 2D list?

611 views
-1

I have a 2D array as below.

``````[[0.0, 0.0, 0.0, 11.36, 20.0, 11.36, 20.02, 0.0],
[13.38, 5.78, 19.94, 5.76, 19.96, 11.28, 13.4, 11.3],
[6.72, 5.78, 13.28, 5.76, 13.3, 11.28, 6.74, 11.3],
[0.06, 5.78, 6.6, 5.76, 6.64, 11.28, 0.08, 11.3],
[13.38, 0.16, 19.94, 0.14, 19.96, 5.66, 13.42, 5.68],
[6.72, 0.16, 13.28, 0.14, 13.3, 5.64, 6.74, 5.68],
[0.04, 0.18, 6.62, 0.14, 6.64, 5.64, 0.08, 5.68],
[0.0, 0.0, 0.0, 11.36, 20.0, 11.36, 20.02, 0.0]]
``````

[0.0, 0.0, 0.0, 11.36, 20.0, 11.36, 20.02, 0.0] is the duplicate array.
I want to remove the 1st duplicate value and keep the last value.
So my output should be;

``````[[13.38, 5.78, 19.94, 5.76, 19.96, 11.28, 13.4, 11.3],
[6.72, 5.78, 13.28, 5.76, 13.3, 11.28, 6.74, 11.3],
[0.06, 5.78, 6.6, 5.76, 6.64, 11.28, 0.08, 11.3],
[13.38, 0.16, 19.94, 0.14, 19.96, 5.66, 13.42, 5.68],
[6.72, 0.16, 13.28, 0.14, 13.3, 5.64, 6.74, 5.68],
[0.04, 0.18, 6.62, 0.14, 6.64, 5.64, 0.08, 5.68],
[0.0, 0.0, 0.0, 11.36, 20.0, 11.36, 20.02, 0.0]]
``````

Any idea how to do this?
Thanks

13

One way would be to use the pandas module:

``````import numpy as np
your_array=np.array([[0.0, 0.0, 0.0, 11.36, 20.0, 11.36, 20.02, 0.0],
[13.38, 5.78, 19.94, 5.76, 19.96, 11.28, 13.4, 11.3],
[6.72, 5.78, 13.28, 5.76, 13.3, 11.28, 6.74, 11.3],
[0.06, 5.78, 6.6, 5.76, 6.64, 11.28, 0.08, 11.3],
[13.38, 0.16, 19.94, 0.14, 19.96, 5.66, 13.42, 5.68],
[6.72, 0.16, 13.28, 0.14, 13.3, 5.64, 6.74, 5.68],
[0.04, 0.18, 6.62, 0.14, 6.64, 5.64, 0.08, 5.68],
[0.0, 0.0, 0.0, 11.36, 20.0, 11.36, 20.02, 0.0]])
``````

to drop the duplicates use:

``````import pandas as pd
df = pd.DataFrame(your_array)
df_without_duplicates = df.drop_duplicates(keep='last')
``````

If you'd prefer numpy array as output, use:

``````new_array = df_without_duplicates.values
``````

Output:

``````array([[13.38,  5.78, 19.94,  5.76, 19.96, 11.28, 13.4 , 11.3 ],
[ 6.72,  5.78, 13.28,  5.76, 13.3 , 11.28,  6.74, 11.3 ],
[ 0.06,  5.78,  6.6 ,  5.76,  6.64, 11.28,  0.08, 11.3 ],
[13.38,  0.16, 19.94,  0.14, 19.96,  5.66, 13.42,  5.68],
[ 6.72,  0.16, 13.28,  0.14, 13.3 ,  5.64,  6.74,  5.68],
[ 0.04,  0.18,  6.62,  0.14,  6.64,  5.64,  0.08,  5.68],
[ 0.  ,  0.  ,  0.  , 11.36, 20.  , 11.36, 20.02,  0.  ]])
``````

posted this
11

Try this: first you have to reverse the list to keep the last element, then a simple for loop will avoid repetition. Finally reverse again the list to have the desired output.

``````# l is your list

l.reverse()
res = []
for x in l:
if x not in res:
res.append(x)
res.reverse()
``````

posted this