python提取重复元素_获取numpy数组中重复元素的所有索引的列表

与往常一样,numpy的解决方案基于^{}的魔力,没有循环或列表理解:records_array = array([1, 2, 3, 1, 1, 3, 4, 3, 2])

idx_sort = argsort(records_array)

sorted_records_array = records_array[idx_sort]

vals, idx_start, count = unique(sorted_records_array, return_counts=True,

return_index=True)

# sets of indices

res = split(idx_sort, idx_start[1:])

#filter them with respect to their size, keeping only items occurring more than once

vals = vals[count > 1]

res = filter(lambda x: x.size > 1, res)

编辑:下面是我之前的回答,需要更多的内存,使用numpy广播和两次调用unique:records_array = array([1, 2, 3, 1, 1, 3, 4, 3, 2])

vals, inverse, count = unique(records_array, return_inverse=True,

return_counts=True)

idx_vals_repeated = where(count > 1)[0]

vals_repeated = vals[idx_vals_repeated]

rows, cols = where(inverse == idx_vals_repeated[:, newaxis])

_, inverse_rows = unique(rows, return_index=True)

res = split(cols, inverse_rows[1:])

与预期一样res = [array([0, 3, 4]), array([1, 8]), array([2, 5, 7])]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值