简体   繁体   English

什么是查找具有所有常见元素的列表集的pythonic方法?

[英]What is a pythonic way to find set of lists which have all common elements?

Say I have a list a=[[1,2],[1.00000001,2.000000001],[2,3],[4,5],[4.0000000002,5.0000006]] , and from that I want to get only a=[[1,2],[2,3],[4,5]] because the other lists have all elements close to some other element of the list.假设我有一个列表a=[[1,2],[1.00000001,2.000000001],[2,3],[4,5],[4.0000000002,5.0000006]] ,我只想得到a=[[1,2],[2,3],[4,5]]因为其他列表的所有元素都靠近列表中的某个其他元素。 How do we do this in Python?我们如何在 Python 中做到这一点? Can set be used in some way?可以以某种方式使用set吗?

I tried using numpy's allclose function but then we have to compare all n(n-1)/2 pairs in the list a, which is clearly inefficient.我尝试使用 numpy 的allclose function 但是我们必须比较列表 a 中的所有 n(n-1)/2 对,这显然是低效的。 Is there a more efficient Pythonic way to do this?有没有更有效的 Pythonic 方式来做到这一点?

Edit: I just used some integers in the example, the inputs are floats in general.编辑:我只是在示例中使用了一些整数,输入通常是浮点数。

If the values are like that (ie what looks like floating point inaccuracy, you can round the values to the precision you're interested in, then use set() to reduce things to unique tuples:如果值是这样的(即看起来像浮点不准确,您可以将值四舍五入到您感兴趣的精度,然后使用set()将事物减少为唯一元组:

>>> a=[[1,2],[1.00000001,2.000000001],[2,3],[4,5],[4.0000000002,5.0000006]]
>>> a_r = [tuple(round(v, 2) for v in pair) for pair in a]
[(1, 2), (1.0, 2.0), (2, 3), (4, 5), (4.0, 5.0)]
>>> set(a_r)
{(2, 3), (4, 5), (1, 2)}

For the data sample in your list and the expected result, as you can use Numpy, i'd suggest to use dtype to convert to integer and use numpy.unique .对于列表中的数据样本和预期结果,因为您可以使用 Numpy,我建议使用dtype转换为 integer 并使用numpy

So, given your list:所以,给定你的清单:

a=[[1,2],[1.00000001,2.000000001],[2,3],[4,5],[4.0000000002,5.0000006]]

You can do as follows:您可以执行以下操作:

import numpy as np

a_np = np.unique(np.array(a, dtype=np.uint), axis=0)

print(a_np)
# [[1 2]
#  [2 3]
#  [4 5]]

暂无
暂无

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

相关问题 查找列表列表的最长公共前缀的 Pythonic 方法是什么? - What is the Pythonic way to find the longest common prefix of a list of lists? 在至少显示在两个子列表中的列表列表中查找公共元素的 Pythonic 方法 - Pythonic way to find common elements in list of lists that show at least in two sub lists 在列表列表中查找所有无的最 Pythonic 方法是什么? - What is the most Pythonic way to find all Nones in a list of lists? 什么是Pythonic方法来获取两个列表元素的所有可能组合的元组列表? - What is a Pythonic way to get a list of tuples of all the possible combinations of the elements of two lists? 从一大组 python 列表中,找到具有最多共同元素的 2 个列表 - Python / Pandas - From a large group of python lists, Find the 2 lists which have the most elements in common - Python / Pandas 以动态pythonic方式查找部分有序集中的最小元素 - Find in a dynamic pythonic way the minimum elements in a partially ordered set 在列表中找到与其他元素不同的元素的最pythonic方法是什么? - what is most pythonic way to find a element in a list that is different with other elements? 更快地查找哪些列表共享元素 - Faster way to find which lists share elements 什么是确保列表中所有元素不同的最pythonic方法? - What's the most pythonic way to ensure that all elements of a list are different? 识别列表列表中共有3个元素的列表 - Identifying lists that have 3 elements in common in a lists of lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM