np.where(label != i)[0]

import torch
import numpy as np
label = torch.tensor([1, 2, 2, 4, 9, 6, 7])
print(label)
a = label.unique()
print(a)
class_num = [[] for i in range(max(label) + 1)]
print(class_num)
for i in a:
    print(i)
    class_num[i] = np.where(label != i)[0]
    print(class_num)

print(class_num)

输出

tensor([1, 2, 2, 4, 9, 6, 7])
tensor([1, 2, 4, 6, 7, 9])
[[], [], [], [], [], [], [], [], [], []]
tensor(1)
[[], array([1, 2, 3, 4, 5, 6], dtype=int64), [], [], [], [], [], [], [], []]
tensor(2)
[[], array([1, 2, 3, 4, 5, 6], dtype=int64), array([0, 3, 4, 5, 6], dtype=int64), [], [], [], [], [], [], []]
tensor(4)
[[], array([1, 2, 3, 4, 5, 6], dtype=int64), array([0, 3, 4, 5, 6], dtype=int64), [], array([0, 1, 2, 4, 5, 6], dtype=int64), [], [], [], [], []]
tensor(6)
[[], array([1, 2, 3, 4, 5, 6], dtype=int64), array([0, 3, 4, 5, 6], dtype=int64), [], array([0, 1, 2, 4, 5, 6], dtype=int64), [], array([0, 1, 2, 3, 4, 6], dtype=int64), [], [], []]
tensor(7)
[[], array([1, 2, 3, 4, 5, 6], dtype=int64), array([0, 3, 4, 5, 6], dtype=int64), [], array([0, 1, 2, 4, 5, 6], dtype=int64), [], array([0, 1, 2, 3, 4, 6], dtype=int64), array([0, 1, 2, 3, 4, 5], dtype=int64), [], []]
tensor(9)
[[], array([1, 2, 3, 4, 5, 6], dtype=int64), array([0, 3, 4, 5, 6], dtype=int64), [], array([0, 1, 2, 4, 5, 6], dtype=int64), [], array([0, 1, 2, 3, 4, 6], dtype=int64), array([0, 1, 2, 3, 4, 5], dtype=int64), [], array([0, 1, 2, 3, 5, 6], dtype=int64)]
[[], array([1, 2, 3, 4, 5, 6], dtype=int64), array([0, 3, 4, 5, 6], dtype=int64), [], array([0, 1, 2, 4, 5, 6], dtype=int64), [], array([0, 1, 2, 3, 4, 6], dtype=int64), array([0, 1, 2, 3, 4, 5], dtype=int64), [], array([0, 1, 2, 3, 5, 6], dtype=int64)]

你可能感兴趣的:(Pytorch学习,python,numpy,深度学习)