Я использую python-3.x, и я хотел бы проверить, существует ли одна из строк массива в my_array_3 в словаре dic_t, пытаясь следующим образом:
my_array_3[2] in dic_t.values()
, но я получаю этоошибка:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Кроме того, я пытаюсь добавить эту пустую строку массива в словарь под ключом "array_1", пытаясь следующим образом:
dic_t["array_1"].append(my_array_3[2])
, но я получаю этоошибка:
AttributeError: 'numpy.ndarray' object has no attribute 'append'
Я не уверен, где проблема и как ее решить, поскольку я пытался разными способами.
полный код:
import numpy as np
# my first array
my_array_1 = np.array([[1 , 2, 3],
[32, 42, 11],
[9, 21, 22],
[9, 21, 22],
[9, 21, 22],
[32, 42, 11],
[1 , 2, 3]])
# my second array
my_array_2 = np.array([[23],[33],[45],[45],[45], [33], [23]])
# here I want to find the unique values index from my_array_1
_, my_array_1_indx = np.unique(my_array_1, return_index=True, axis=0)
# here I want to returen the unique values sorted from my_array_1
my_array_1_uniq = (my_array_1[np.sort(my_array_1_indx)])
# here I want to returen the unique values sorted from my_array_2 based on the my_array_1_indx
my_array_2_uniq = (my_array_2[np.sort(my_array_1_indx)])
#save the result to dictionary
dic_t= {"array_1": my_array_1_uniq,
"array_2": my_array_2_uniq}
my_array_3 = np.array([[23 , 4, 2],
[32, 42, 11],
[54, 1, 9],
[9, 21, 22],
[9, 21, 22],
[34, 67, 77],
[1 , 2, 3]])
# now I want to check if one of the rows in my_array_3 exists in the dictionary dic_t
for i in range (len(my_array_3)):
# here the first error ******
if my_array_3[i] in dic_t.values():
print ( my_array_3[i], " deleted")
my_array_3 = np.delete(my_array_3, (i), axis=0)
else:
print ( my_array_3[i], " added")
# here the second error ******
dic_t["array_1"].append(my_array_3[i])