Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中或使用numpy获取具有相同元素的行的索引_Python_Numpy - Fatal编程技术网

在python中或使用numpy获取具有相同元素的行的索引

在python中或使用numpy获取具有相同元素的行的索引,python,numpy,Python,Numpy,我试图在一行中找到具有相同项的元素索引 以下是我目前的代码: from __future__ import print_function import numpy as np b = np.array([[0, 0, 0], [2, 2, 2], [2, 2, 2], [2, 0, 2], [2, 0, 2]]) for rows in b: if len(set(rows)) <= 1:

我试图在一行中找到具有相同项的元素索引

以下是我目前的代码:

 from __future__ import print_function
 import numpy as np


 b = np.array([[0, 0, 0],
          [2, 2, 2],
          [2, 2, 2],
          [2, 0, 2],
          [2, 0, 2]])

for rows in b:
    if len(set(rows)) <= 1:
        indices = list(zip(*np.where(rows == 2)))
        print(indices)
from\uuuuu future\uuuuu导入打印功能
将numpy作为np导入
b=np.array([[0,0,0],
[2, 2, 2],
[2, 2, 2],
[2, 0, 2],
[2, 0, 2]])
对于b中的行:

如果len(set(rows))给出示例,您的预期结果是什么?请记住有两个相同的行(忽略边缘上的0)。因此,这个:
[(1,),(2,),(3,)][(1,),(2,),(3,)]
为了更好地反映这一点,@sender在这个问题的版本之前,我编辑了我的代码,@sender,只要求与包含所有相同值的行对应的行索引,这是一个答案:
eq=np.equal.reduce(b,轴=1.nonzero()
。您可以将结果作为索引元组传递:
assert(b[eq]=[[2,2,2],[2,2,2]])。all()==True
。是否有方法获取表示行和列索引的元组?i、 e.(x,y)给定示例,您的预期结果是什么?请记住有两行相同(忽略边缘上的0)。因此,这个:
[(1,),(2,),(3,)][(1,),(2,),(3,)]
为了更好地反映这一点,@sender在这个问题的版本之前,我编辑了我的代码,@sender,只要求与包含所有相同值的行对应的行索引,这是一个答案:
eq=np.equal.reduce(b,轴=1.nonzero()
。您可以将结果作为索引元组传递:
assert(b[eq]=[[2,2,2],[2,2,2]])。all()==True
。是否有方法获取表示行和列索引的元组?i、 e.(x,y)
import numpy as np

b = np.array([[0, 0, 0],
              [2, 2, 2],
              [2, 2, 2],
              [2, 0, 2],
              [2, 0, 2]])

for rows in b:
    if len(set(rows)) <= 1:
        indices = list(zip(*np.where(rows == 2)))
        rows_to_list = rows.tolist()
        b_to_list = b.tolist()
        if indices != []:
            print(b_to_list.index(rows_to_list))
            print(indices)
import pandas as pd

b = pd.DataFrame(b)

print b[b.duplicated()]