Remove unnecessary items from python list

daddyodevil

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. 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. So if I have a list of 4 points, it means I already have the other four points as well. So for every combination of 4 points I am trying to remove the other 4 points from the list.

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. 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.

Is there a way to directly achieve this maybe in one or two lines without loops and such?

Thanks in advance.

Thierry Lathuille

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.

A demonstration, using the numbers from 1 to 8 instead of tuples for readability:

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. 

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))

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Make gnuplot remove unnecessary text from the graph

分類Dev

Remove a list from a list of lists Python

分類Dev

Best way to iterate over a list and remove items from it?

分類Dev

remove duplicate items from list if they come in certain sequence

分類Dev

Remove repeated sequence of integers from list in Python

分類Dev

Python 2.7: Remove subdomains from list

分類Dev

checking list<> items and if it contains same item copy them and remove it from list<>

分類Dev

How to list items in a list and remove duplicates in ReactJS?

分類Dev

Remove all items from RecyclerView

分類Dev

Remove unselected items from the array

分類Dev

make new python lists with indexes from items in a single list

分類Dev

Python - print items that are not in a list

分類Dev

Combination items of List in Python

分類Dev

How can i create a list (new) from an existing list where items of list(new) will be list in python

分類Dev

Python items in a list still a list?

分類Dev

Remove unnecessary fields in ElasticSearch

分類Dev

Remove Duplicate values from a list while keeping track of the removed items' indexes

分類Dev

How I can remove more than 1 items from drop down list?

分類Dev

How to make nested list from list where values are counts of list items Python

分類Dev

Remove spaces between items returned in a list

分類Dev

Grouping and remove from list

分類Dev

How to remove the prefix from each element of python list?

分類Dev

How to remove duplicate combinations tuples from python list?

分類Dev

How to remove '!' from the end of each string in Python list?

分類Dev

Setting items from a list to another list (both containing class instances) in random groups in python

分類Dev

Excluding items from a list that exist in another list

分類Dev

Remove unnecessary white space in html

分類Dev

Remove items from custom arraylist implemetation without .remove

分類Dev

Remove duplicates from list of object

Related 関連記事

  1. 1

    Make gnuplot remove unnecessary text from the graph

  2. 2

    Remove a list from a list of lists Python

  3. 3

    Best way to iterate over a list and remove items from it?

  4. 4

    remove duplicate items from list if they come in certain sequence

  5. 5

    Remove repeated sequence of integers from list in Python

  6. 6

    Python 2.7: Remove subdomains from list

  7. 7

    checking list<> items and if it contains same item copy them and remove it from list<>

  8. 8

    How to list items in a list and remove duplicates in ReactJS?

  9. 9

    Remove all items from RecyclerView

  10. 10

    Remove unselected items from the array

  11. 11

    make new python lists with indexes from items in a single list

  12. 12

    Python - print items that are not in a list

  13. 13

    Combination items of List in Python

  14. 14

    How can i create a list (new) from an existing list where items of list(new) will be list in python

  15. 15

    Python items in a list still a list?

  16. 16

    Remove unnecessary fields in ElasticSearch

  17. 17

    Remove Duplicate values from a list while keeping track of the removed items' indexes

  18. 18

    How I can remove more than 1 items from drop down list?

  19. 19

    How to make nested list from list where values are counts of list items Python

  20. 20

    Remove spaces between items returned in a list

  21. 21

    Grouping and remove from list

  22. 22

    How to remove the prefix from each element of python list?

  23. 23

    How to remove duplicate combinations tuples from python list?

  24. 24

    How to remove '!' from the end of each string in Python list?

  25. 25

    Setting items from a list to another list (both containing class instances) in random groups in python

  26. 26

    Excluding items from a list that exist in another list

  27. 27

    Remove unnecessary white space in html

  28. 28

    Remove items from custom arraylist implemetation without .remove

  29. 29

    Remove duplicates from list of object

ホットタグ

アーカイブ