Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a sublist from a list that meets a certain condition

Tags:

python

I create all three-element permutations without mirroring, using itertools.product():

import itertools

list_1 = [list(i) for i in itertools.product(tuple(range(4)), repeat=3) if tuple(reversed(i)) >= tuple(i)]

Output:

[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 1, 3], [0, 2, 0], [0, 2, 1], [0, 2, 2], [0, 2, 3], [0, 3, 0], [0, 3, 1], [0, 3, 2], [0, 3, 3], [1, 0, 1], [1, 0, 2], [1, 0, 3], [1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 3, 1], [1, 3, 2], [1, 3, 3], [2, 0, 2], [2, 0, 3], [2, 1, 2], [2, 1, 3], [2, 2, 2], [2, 2, 3], [2, 3, 2], [2, 3, 3], [3, 0, 3], [3, 1, 3], [3, 2, 3], [3, 3, 3]]

How do I delete these sublisters from the list list_1, which have the same number of corresponding values and then leave only one of them?

For example, in sublists [1,1,2], [1,2,1] the number of given values is the same in all, that is, in each sub-list there are two 1 and one 2, that's why I consider the sublisters to be the same and that's why I want to leave only the first one, namely [1,1,2]. How can this be done?

I was thinking about counting the number of corresponding values in each sub-list and creating a list with the occurring feature regarding the amount of given values, and then checking each element from the list list_1 in the loop or the element with the given feature has not occurred before. But it seems to me to be very complicated.

like image 845
Tomasz Przemski Avatar asked Dec 13 '25 10:12

Tomasz Przemski


2 Answers

Rather than using product from the itertools module, use combinations_with_replacement. That does what you want in one line without any massaging afterward:

list1 = [list(i) for i in combinations_with_replacement(range(4),3)]

The result of print(list1) after that is

[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 1, 1], [0, 1, 2], [0, 1, 3], [0, 2, 2], [0, 2, 3], [0, 3, 3], [1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]]

Note that your conversion of the range object to a tuple is not necessary.

like image 109
Rory Daulton Avatar answered Dec 15 '25 23:12

Rory Daulton


This might do the trick:

import itertools

def uniqifyList(list):
    indexToReturn = []
    sortedUniqueItems = []

    for idx, value in enumerate(list):
        # check if exists in unique_list or not
        value.sort()
        if value not in sortedUniqueItems:
            sortedUniqueItems.append(value)
            indexToReturn.append(idx)

    return [list[i] for i in indexToReturn]

list1 = [list(i) for i in itertools.product(tuple(range(4)), repeat=3) if tuple(reversed(i)) >= tuple(i)]
print(list1)

list2 = uniqifyList(list1)
print(list2)

Which outputs:

[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 1, 1], [0, 1, 2], [0, 1, 3], [0, 2, 2], [0, 2, 3], [0, 3, 3], [1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]]
like image 27
Jonathan Avatar answered Dec 16 '25 00:12

Jonathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!