I have a python list called added
that contains 156 individual lists containing two cols references and an array. An example is as follows:
[0, 1, array]
The problem is I have duplicates, although they are not exact as the column references will be flipped. The following two will be exactly the same:
[[0, 1, array], [1, 0, array]]
The way I have tried removing duplicates was to sort the numbers and check if any were the same and if so then append the result to a new list.
Both resulted in separate errors:
for a in range(len(added)):
added[a][0:2] = added[a][0:2].sort()
TypeError: can only assign an iterable
I also tried to see if the array was in my empty python list no_dups
and if it wasnt then append the column refernces and array.:
no_dups = []
for a in range(len(added)):
if added[a][2] in no_dups:
print('already appended')
else:
no_dups.append(added[a])
<input>:2: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
Neither worked. I'm struggling to get my head round how to remove duplicates here.
Thanks.
EDIT: reproducible code:
import numpy as np
import pandas as pd
from sklearn import datasets
data = datasets.load_boston()
df = pd.DataFrame(data.data, columns=data.feature_names)
X = df.to_numpy()
cols = []
added = []
for column in X.T:
cols.append(column)
for i in range(len(cols)):
for x in range(len(cols)):
same_check = cols[i] == cols[x]
if same_check.all() == True:
continue
else:
added.append([i, x, cols[i] * cols[x]])
This code should give you access to the entire created added
list.
added
array would help.