简体   繁体   English

如何在列表中查找常见元素

[英]How to find common elements inside a list

I have a list l1 that looks like [1,2,1,0,1,1,0,3..]. 我有一个看起来像[1,2,1,0,1,1,0,3 ..]的列表l1。 I want to find, for each element the indexes of elements which have same value as the element. 我想为每个元素查找与该元素具有相同值的元素索引。

For eg, for the first value in the list, 1, it should list out all indexes where 1 is present in the list and it should repeat same for every element in the list. 例如,对于列表中的第一个值1,它应该列出列表中存在1的所有索引,并且应该对列表中的每个元素重复相同的索引。 I can wrote a function to do that iterating through the list but wanted to check if there is any predefined function. 我可以编写一个函数来遍历列表,但想检查是否有任何预定义的函数。

I am getting the list from Pandas dataframe columns, it would be good know if series/dataframe library offer any such functions 我是从Pandas数据框列中获取列表的,所以最好知道series / dataframe库是否提供任何此类功能

You can use numpy.unique , which can return the inverse too. 您可以使用numpy.unique ,它也可以返回相反的值。 This can be used to reconstruct the indices using numpy.where : 可以使用numpy.where来重建索引:

In [49]: a = [1,2,1,0,1,1,0,3,8,10,6,7]

In [50]: uniq, inv = numpy.unique(a, return_inverse=True)

In [51]: r = [(uniq[i], numpy.where(inv == i)[0]) for i in range(uniq.size)]

In [52]: print(r)
[(0, array([3, 6])), (1, array([0, 2, 4, 5])), (2, array([1])), (3, array([7])), (6, array([10])), (7, array([11])), (8, array([8])), (10, array([9]))]   

i tried brute force..may be u can optimize 我尝试过蛮力..也许你可以优化

here is python3 code 这是python3代码

L = [1,2,1,0,1,1,0,3]
D = dict()
for i in range(len(L)):
    n =[]
    if L[i] not in D.keys():
        for j in range(len(L)):
            if L[i] == L[j]:
                n.append(j)
        D[L[i]] = n
for j in D.keys():
    print(j,"->",D.get(j))  

You can also try something like this: 您也可以尝试如下操作:

import pandas as pd
df = pd.DataFrame({'A': [1,2,1,0,1,1,0,3]})
uni = df['A'].unique()
for i in uni:
    lists = df[df['A'] == i].index.tolist()
    print(i, '-->', lists)

Output: 输出:

1 --> [0, 2, 4, 5]
2 --> [1]
0 --> [3, 6]
3 --> [7]

You could achieve this using a defaultdict . 您可以使用defaultdict实现此目的。

from collection import defaultdict

input = [1,2,1,0,1,1,0,3]
#Dictionary to store our indices for each value
index_dict = defaultdict(list)
#Store index for each item
for i, item in enumerate(input):
    index_dict[item].append(i)

If you want a list which contains the indices of elements which are the same as the corresponding element in your input list, you can just create a reference to the dictionary: 如果想要一个包含与输入列表中的相应元素相同的元素索引的列表,则只需创建对字典的引用即可:

same_element_indices = [index_dict[x] for x in input]

This has the advantage of only referencing the one object for each identical element. 这样做的好处是,每个相同的元素仅引用一个对象。

Output would be: 输出为:

[[0, 2, 4, 5], 
 [1], 
 [0, 2, 4, 5], 
 [3, 6], 
 [0, 2, 4, 5], 
 [0, 2, 4, 5], 
 [3, 6], 
 [7]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM