Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 3.x 是否有python函数从唯一值获取所有索引?_Python 3.x_List_Unique - Fatal编程技术网

Python 3.x 是否有python函数从唯一值获取所有索引?

Python 3.x 是否有python函数从唯一值获取所有索引?,python-3.x,list,unique,Python 3.x,List,Unique,我知道有像set()或np.unqiue()这样的方法可以从列表中获取唯一值。但是我在寻找一种方法来获取不超过一次的值的索引 示例=[0,1,1,2,3,3,4] 我要找的是 所需索引列表=[0,3,6] 有什么建议吗?不知道任何预构建的解决方案,可能需要创建自己的解决方案。有不同的方法可以实现这一点,但是使用经典的Python实现,您可以轻松创建count_dict,并从原始列表中筛选count为1的值 >>> from collections import Counter

我知道有像set()或np.unqiue()这样的方法可以从列表中获取唯一值。但是我在寻找一种方法来获取不超过一次的值的索引

示例=[0,1,1,2,3,3,4]

我要找的是
所需索引列表=[0,3,6]


有什么建议吗?

不知道任何预构建的解决方案,可能需要创建自己的解决方案。有不同的方法可以实现这一点,但是使用经典的Python实现,您可以轻松创建count_dict,并从原始列表中筛选count为1的值

>>> from collections import Counter
>>> example = [0,1,1,2,3,3,4]
>>> counted = Counter(example)
>>> desired_index_list = [index for index, elem in enumerate(example) if counted[elem] == 1]
>>> desired_index_list
[0, 3, 6]

不知道任何预构建的解决方案,可能您需要创建自己的解决方案。有不同的方法可以实现这一点,但是使用经典的Python实现,您可以轻松创建count_dict,并从原始列表中筛选count为1的值

>>> from collections import Counter
>>> example = [0,1,1,2,3,3,4]
>>> counted = Counter(example)
>>> desired_index_list = [index for index, elem in enumerate(example) if counted[elem] == 1]
>>> desired_index_list
[0, 3, 6]

您可以将其作为一行代码和一个列表:

from collections import Counter
[example.index(x) for x, y in Counter(example).items() if y == 1]

(使用计数器,返回每个项的元组(x)及其出现次数(y),如果项的计数为1,则返回该项的索引)。

您可以将此作为一个带有列表的一行代码来执行:

from collections import Counter
[example.index(x) for x, y in Counter(example).items() if y == 1]
(使用计数器,返回每个项的元组(x)及其出现次数(y),如果该项的计数为1,则返回该项的索引)