简体   繁体   English

将唯一ID分配给python中的列表列表,其中重复项获得相同的id

[英]Assign unique id to list of lists in python where duplicates get the same id

I have a list of lists (which can contain up to 90k elements) 我有一个列表列表(最多可包含90k个元素)

[[1,2,3], [1,2,4], [1,2,3], [1,2,4], [1,2,5]]

I would like to assign an id to each elements, where the id is unique, except when the item is duplicated. 我想为每个元素分配一个id,其中id是唯一的,除非项目是重复的。 So for the list above, I'd need this back: 所以对于上面的列表,我需要这个:

[0,1,0,1,2]

What is the most effective way to do this? 这样做最有效的方法是什么?

Keep a map of already seen elements with the associated id. 使用关联的ID保留已经看到的元素的映射。

from itertools import count
from collections import defaultdict


mapping = defaultdict(count().__next__)
result = []
for element in my_list:
    result.append(mapping[tuple(element)])

you could also use a list-comprehension: 你也可以使用列表理解:

result = [mapping[tuple(element)] for element in my_list]

Unfortunately list s aren't hashable so you have to convert them to a tuple when storing them as keys of the mapping. 遗憾的是list不可清除,因此在将它们存储为映射的键时必须将它们转换为tuple


Note the trick of using defaultdict , and count().__next__ to provide unique increasing ids. 注意使用defaultdictcount().__next__以提供独特的增加ID。 On python2 you have to replace .__next__ with .next . 在python2上,你必须用.next替换.__next__

The defaultdict will assign a default value when it cannot find a key. defaultdict将在找不到密钥时指定默认值。 The default value is obtained by calling the function provided in the constructor. 通过调用构造函数中提供的函数获取默认值。 In this case the __next__ method of the count() generator yields increasing numbers. 在这种情况下, count()生成器的__next__方法会产生越来越多的数字。

As a more portable alternative you could do: 作为一种更便携的替代方案,您可以:

from functools import partial

mapping = defaultdict(partial(next, count()))

An alternative solution, as proposed in the comments, is to just use the index as unique id: 如评论中所提出的另一种解决方案是仅将索引用作唯一ID:

result = [my_list.index(el) for el in my_list]

This is imple however: 但这是实现:

  • It takes O(N^2) time instead of O(N) 它需要O(N ^ 2)时间而不是O(N)
  • The ids are unique, increasing but not consecutive (which may or may not be a problem) ID是唯一的,增加但不连续(可能是也可能不是问题)

For comparison of the two solutions see: 有关两种解决方案的比较,请参阅:

In [1]: from itertools import count
   ...: from collections import defaultdict

In [2]: def hashing(seq):
   ...:         mapping = defaultdict(count().__next__)
   ...:         return [mapping[tuple(el)] for el in seq]
   ...: 

In [3]: def indexing(seq):
   ...:    return [seq.index(i) for i in seq]
   ...: 

In [4]: from random import randint

In [5]: seq = [[randint(1, 20), randint(1, 20), randint(1, 20)] for _ in range(90000)]

In [6]: %timeit hashing(seq)
10 loops, best of 3: 37.7 ms per loop

In [7]: %timeit indexing(seq)
1 loop, best of 3: 26 s per loop

Note how for a 90k element list the mapping solution takes less 40 milliseconds while the indexing solution takes 26 seconds . 请注意,对于90k元素列表,映射解决方案需要的时间少于40 毫秒,而索引解决方案需要26

This is how I approached it: 这就是我接近它的方式:

from itertools import product
from random import randint
import time

t0 = time.time()
def id_list(lst):
    unique_set = set(tuple(x) for x in lst)
    unique = [list(x) for x in unique_set]
    unique.sort(key = lambda x: lst.index(x))

    result = [unique.index(i[1]) for i in product(lst, unique) if i[0] == i[1]]

    return result

seq = [[randint(1, 5), randint(1, 5), randint(1, 5)] for i in range(90000)]

print(id_list(seq))

t1 = time.time()

print("Time: %.4f seconds" % (t1-t0))

Which prints out the sequence of ids, along with an approximate time it took to compute a sequence of random integers in a list between 1 and 4 , 90000 times. 打印出ID的序列,大致花费的时间计算在14,90000倍之间的名单随机整数序列一起。

Time: 2.3397 seconds  # Will slightly differ from computation to computation

The actual time will always be a bit higher, since it needs to be accounted for in the print statement at the end, but it should not be too much of a difference. 实际时间总是会高一些,因为它最后需要在print语句中考虑,但它不应该太大差别。

I also used the time library to label the time intervals between the start and the end of the code block. 我还使用time库来标记代码块的开始和结束之间的时间间隔。

import time

t0 = time.time()

# code block here

t1 = time.time()

# Difference in time: t1 - t0 

The itertools library along with product used in the code segment will speed up the computation too. itertools库以及代码段中使用的product也将加速计算。

I slight modification of Bakuriu's solution that works only with numpy arrays, it works better in terms of memory footprint and computation (as it does need to cast arrays to tuples): 我稍微修改了Bakuriu的解决方案,该解决方案只适用于numpy数组,它在内存占用和计算方面效果更好(因为它需要将数组转换为元组):

from itertools import count
from collections import defaultdict
from functools import partial

def hashing_v1(seq):
    mapping = defaultdict(partial(next, count()))
    return [mapping[tuple(el)] for el in seq]

def hashing_v2(seq):
    mapping = defaultdict(partial(next, count()))
    result = []
    for le in seq:
        le.flags.writeable = False
        result.append(mapping[le.data])
    return result

In [4]: seq = np.random.rand(50000, 2000)

In [5]: %timeit hashing_v1(seq)
1 loop, best of 3: 14.1 s per loop

In [6]: %timeit hashing_v2(seq)
1 loop, best of 3: 1.2 s per loop

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

相关问题 如何从pandas中的列创建唯一ID列表,其中ID列表在Python中被提及为字符串 - How to create a list of unique ID from a column in pandas where lists of ID are mentioned as strings in Python 在Python中更改的lambda列表中分配可检索的唯一ID? - assign a retrievable unique ID to a changing lambda list in python? 通过id删除Python列表中的重复项 - Removing duplicates in a Python list by id 为每个唯一 ID 分配相同的值 - Assign same value for every unique ID 在python中获取计算机的唯一ID - Get unique id of computer in python Python:如何获取唯一 ID 并从第 1 列(ID)和第 3 列(描述)中删除重复项,然后在 Pandas 中获取第 2 列(值)的中值 - Python: how to get unique ID and remove duplicates from column 1 (ID), and column 3 (Description), Then get the median for column 2 (Value) in Pandas 比较列表-删除ID = ID和日期>日期的列表 - Compare lists - delete list where id = id and date > date 在python django模块中删除content_id和ip_address相同的重复项 - Remove duplicates where content_id and ip_address are the same In python django module 如何从包含重复项的ID列表中生成唯一ID - How to generate the unique id from a list of ids containing duplicates 如何在 python 中列出具有相同 ID 的 select 列表? - How to select lists with the same id in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM