简体   繁体   English

从python列表中删除不必要的项目

[英]Remove unnecessary items from python list

I have a list of 8 unique tuples, each of size 2. From there I find all possible combinations of size 4. Now, I have a list of lists, and in each of the sublists I have exactly 4 tuples.我有一个包含 8 个唯一元组的列表,每个元组的大小为 2。从那里我找到了大小为 4 的所有可能组合。现在,我有一个列表列表,在每个子列表中我正好有 4 个元组。 Something like -就像是 -

[[(1, 2), (4, 5), (223, 456), (111, 345)], [...], ...]

Where the main list maybe -主要列表可能在哪里 -

[(1, 2), (4, 5), (223, 456), (111, 345), (123, 4), (23, 89), (999, 888), (895, 569)]

These are actually co-ordinates of points in a 2D plane and I am dividing the 8 points in two sets of 4 each.这些实际上是 2D 平面中点的坐标,我将 8 个点分成两组,每组 4 个。 So if I have a list of 4 points, it means I already have the other four points as well.因此,如果我有一个包含 4 分的列表,则意味着我已经拥有其他 4 分。 So for every combination of 4 points I am trying to remove the other 4 points from the list.因此,对于 4 点的每个组合,我都试图从列表中删除其他 4 点。

The following works -以下作品——

def generate_all_points(points, size):
    from itertools import combinations

    all_combinations = combinations(points, size)
    return (list(all_combinations))    

def generate_4points(points):

    all_4points = generate_all_points(points, 4)
    print (len(all_4points))
    print ("ALL POINTS -\t", points)
    for point_set in all_4points:

        to_remove = list(set(points).difference(set(point_set)))
        for item in all_4points:
            if (len(set(to_remove).difference(item)) == 0):
                all_4points.remove(item)

        print ("Main -\t", point_set, "\nTo Remove -\t", to_remove)
        #all_4points.remove(list(set(points).difference(set(point_set))))
    print (len(all_4points))

I tried using just the set.difference but it reorders the list items and thus I can't remove them directly.我尝试只使用set.difference但它重新排序列表项,因此我无法直接删除它们。 That I tried in the commented line.我在注释行中尝试过。 What I did was find the remaining 4 points from the 8, then if the length of the set difference of the 4 and any item in the list of combinations is zero, then that means together, both set of 4 points match up to the unique 8, thus I remove that particular item.我所做的是从 8 中找到剩余的 4 点,然后如果 4 和组合列表中的任何项目的集合差的长度为零,那么这意味着在一起,这两个 4 点的集合匹配唯一8,因此我删除了该特定项目。

Is there a way to directly achieve this maybe in one or two lines without loops and such?有没有办法在没有循环等的情况下在一两行中直接实现这一点?

Thanks in advance.提前致谢。

A better solution is to avoid generating the unwanted combinations in the first place.更好的解决方案是首先避免生成不需要的组合。

This is in fact rather easy, as combinations are generated in order.这实际上相当容易,因为组合是按顺序生成的。 We just have to take the first half of them.我们只需要拿他们的前半部分。 There are 70 combinations of 4 points among 8 (8*7*6*5/(4*3*2)), so we just keep the first 35 ones. 8个(8*7*6*5/(4*3*2))中4个点有70个组合,所以我们只保留前35个。

A demonstration, using the numbers from 1 to 8 instead of tuples for readability:一个演示,使用从 1 到 8 的数字而不是元组以提高可读性:

from itertools import combinations, islice

l = [1, 2, 3, 4, 5, 6, 7, 8]

nb_combinations = 70
print(list(islice(combinations(l, 4), nb_combinations//2)))

Output:输出:

[(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 3, 6), (1, 2, 3, 7), (1, 2, 3, 8), 
(1, 2, 4, 5), (1, 2, 4, 6), (1, 2, 4, 7), (1, 2, 4, 8), (1, 2, 5, 6), 
(1, 2, 5, 7), (1, 2, 5, 8), (1, 2, 6, 7), (1, 2, 6, 8), (1, 2, 7, 8),
(1, 3, 4, 5), (1, 3, 4, 6), (1, 3, 4, 7), (1, 3, 4, 8), (1, 3, 5, 6),
(1, 3, 5, 7), (1, 3, 5, 8), (1, 3, 6, 7), (1, 3, 6, 8), (1, 3, 7, 8),
(1, 4, 5, 6), (1, 4, 5, 7), (1, 4, 5, 8), (1, 4, 6, 7), (1, 4, 6, 8),
(1, 4, 7, 8), (1, 5, 6, 7), (1, 5, 6, 8), (1, 5, 7, 8), (1, 6, 7, 8)]

You can see that all of these 35 combinations contain the first value 1, so we are certain that none of them is the complementary of another one of this set.您可以看到所有这 35 个组合都包含第一个值 1,因此我们确定它们中没有一个是该集合中另一个的补充。

So, your function could be written:所以,你的函数可以写成:

from itertools import combinations, islice

def generate_4points(points):
    # we only keep the first 35 combinations out of 70
    return list(islice(combinations(points, 4), 35))

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

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