我有一個二維陣列形式
[[ 1 6]
[ 2 7]
[ 5 6]
[ 6 1]
[ 6 5]
[ 7 2]]
我想要表單的輸出
[[ 1 6]
[ 2 7]
[ 5 6]]
我怎樣才能得到這個輸出?
uj5u.com熱心網友回復:
這是使用普通 Python 的解決方案,但我確信 NumPy 有更慣用的方法。
import numpy as np
a = np.array([
[1, 6],
[2, 7],
[5, 6],
[6, 1],
[6, 5],
[7, 2]])
seen = set()
out = []
for row in a:
elements = frozenset(row)
if elements not in seen:
seen.add(elements)
out.append(row)
print(np.array(out))
輸出:
[[1 6]
[2 7]
[5 6]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342697.html
