Find symmetric pairs quickly in numpy

后端 未结 6 1827
抹茶落季
抹茶落季 2020-12-03 17:44
from itertools import product
import pandas as pd

df = pd.DataFrame.from_records(product(range(10), range(10)))
df = df.sample(90)
df.columns = \"c1 c2\".split()
df         


        
6条回答
  •  天涯浪人
    2020-12-03 18:15

    If this needs to be fast, and if your variables are integer, then the following trick may help: let v,w be the columns of your vector; construct [v+w, np.abs(v-w)] =: [x, y]; then sort this matrix lexicographically, remove duplicates, and finally map it back to [v, w] = [(x+y), (x-y)]/2.

提交回复
热议问题