How to get all possible combinations of pairs with unique elements [duplicate] - python

This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 6 months ago.
I have a list of even N elements and would like to get a list of lists of all possible and unique pairs, e.g.:
list = [1, 2, 3, 4, 5, 6]
result: [[(1,2), (3,4), (5,6)], [(1,2), (3,5), (4,6)] ...
So each list I get as the result should have N/2 elements (pairs with unique numbers). This question seemed similar to my problem, although the answer gives the lists with 2 combinations only and it doesn't work for N > 4; not sure if it's possible to rework this solution for my purposes.
I suppose that one possible option is to:
iterate through each possible order of N numbers (123456, 123465 ... 654321)
create a list of pairs for each following 2 elements ([1,2,3,4,5,6] -> [(12), (34), (56)])
sort those pairs and eliminate duplicates
But it feels that there should be a more elegant solution, would be grateful for help :)

Unless I'm mistaken,
l = [1, 2, 3, 4, 5, 6]
x = list(itertools.combinations(itertools.combinations(l, 2), 3))
does what you want.

Related

Take list of values and find biggest combination between certain amount of items in the list [duplicate]

This question already has answers here:
How to find k biggest numbers from a list of n numbers assuming n > k
(5 answers)
Closed 10 months ago.
I have a list of values that has a size of 24.
What I want to do is find the biggest combination using three values from the list. I only want to use three of the values, the highest ones possible.
So if I had a list that looked like:
vals_list = [5, 3, 5, 5, 5, 4, 2, 1, 2, 4]
My desired output would be 15, as it would take three of the '5' values in the list and sum them together. Is there any possible way to do this in python?
IIUC sorting values and extract last 3 values for top3, last sum them:
print (sum(sorted(vals_list)[-3:]))
15

Efficient way to remove all items from another list [duplicate]

This question already has answers here:
Remove all the elements that occur in one list from another
(13 answers)
How do I subtract one list from another?
(16 answers)
Closed 2 years ago.
I have a list of items. I also have another list of items (subset of original list) I want to be removed from this list.
myitems = [1, 1, 2, 3, 3, 4, 5]
items_to_remove = [1, 4]
The output of this should be [2, 3, 3, 5]
What is the most efficient way all items from items_to_remove from myitems?
My current code is:
for item in items_to_remove:
myitems = list(filter((item).__ne__,myitems)
Because my actual use case has lots of items to be removed I am trying to find a more efficient way to do this.
The most efficient way is to create a set of the items to be removed, then use the set to filter the first list:
s = set(items_to_remove)
result = [x for x in myitems if x not in s]
With the sample list values, this produces the desired result:
[2, 3, 3, 5]
This solution has O(l1+l2) time complexity, where l1 and l2 are the two list lengths.
Note that some of the answers in the duplicate posts skipped the set creation, and just tested for membership directly in the second list. While correct, this has a serious negative impact on performance if the second list is large, with the performance being O(l1*l2) where l1 and l2 are the two list lengths. So unless the second list is very small, you definitely want to convert it to a set first.

How does set() remove duplicates from a list [duplicate]

This question already has answers here:
Removing duplicates in lists
(56 answers)
'order' of unordered Python sets
(5 answers)
Closed 3 years ago.
I tried to remove duplicates from a list in Python 3 by converting it into a set by using set(). However I tried to achieve a certain order at the end of the process. After converting the list, I noticed, that the resulting set was not in the order, I would have expected.
data = [3, 6, 3, 4, 4, 3]
my_set = set(data)
print(my_set)
The resulting set is: (3,4,6)
I expected set() to kind of iterate over the given list from 0 to n, keeping the first instance of every integer it encounters. However the resulting set seems to be ordered in a different way.
I was unable to find anything about this in the python documentation, or here on stack overflow. Is it known how the set() method orders the elements in the given datastructure when converting it to a set?
The concept of order simply does not exist for sets in Python, which is why you can not expect the elements to be shown in any particular order. Here is an example of creating a list without duplicates, that has the same order as the original list.
data = [3, 6, 3, 4, 4, 3]
without_duplicates = list(dict.fromkeys(data))
>>> without_duplicates
[3, 6, 4]
set objects are not ordered by key or by insertion order in Python... you can however get what you want by building the result you are looking for explicitly:
res = []
seen = set()
for x in data:
if x not in seen:
seen.add(x)
res.append(x)
print(res)

Find every combination of a list of integers split into exactly two subsets [duplicate]

This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 6 years ago.
Suppose I have a list of integers:
myList = [1,2,3,4]
How would one return every possible combination of these two subsets with Python.
For example, set [1,2,3,4] would produce
[1,2], [3,4] and [1,3], [2,4] and [1,4],[2,3]
itertools can give you all combinations of a list of size you define (here n):
import itertools
myList = [1,2,3,4]
n=3
for subset in itertools.combinations(myList , n):
print(subset)
output:
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)

Get unique combinations of elements from a python list [duplicate]

This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 9 years ago.
Edit:
This is not a exact duplicate of How to get all possible combinations of a list’s elements?
This topic is about finding unique combinations while the other topic is about finding ALL combinations.
If I have a python list:
L = [1,2,3,4]
what's the best way to get all the possible unique combinations of 3 elements from the list like below:
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
The order of the elements in the combinations doesn't matter. For example, "1,2,3" and "3,2,1" will be considered the same combination.
I can probably write a few loops to do this but I think there might be a one-liner which can do the same.
You need itertools.combinations:
>>> from itertools import combinations
>>> L = [1, 2, 3, 4]
>>> [",".join(map(str, comb)) for comb in combinations(L, 3)]
['1,2,3', '1,2,4', '1,3,4', '2,3,4']

Categories

Resources