Python - How to get all possible combinations from a nested list


Hamza Arshad:

I have nested lists like this:

values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]]

How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5?

This is what I'm using:

def getCombinations(values, min_len):
    combo = "\n"
    numbers = []
    doc = {}
    for val in values:
        doc[val[0]] = val[1]
        numbers.append(val[1])

    result = [seq for i in range(len(numbers), 0, -1) for seq in itertools.combinations(numbers, i) if sum(seq) >= 0.5]
    temp = doc.copy()

    for r in result:
        doc = temp.copy()
        if len(r) >= min_len:
            for rr in r:
                combo = combo + get_key(doc, rr) + " "

                doc.pop(get_key(doc, rr))
            combo = combo + "\n"
    return combo

I have some problems with my algorithm when there are multiple values ​​in the above list (eg 0.2).

Currently, it returns as min_length=3:

Total Equinor Petoro DNO
Total Equinor Petoro 
Total Equinor Petoro 
Total Petoro DNO 
Equinor Total Petoro 
Upps:

You can use a list comprehension like this:

illustrate:

  • The first fordefines the length of the combination. Use 2 to each length values.
  • The second forcreates the actual combination
  • After ifusing the generative method, sum the number of items
from itertools import combinations

combis = [
    item
    for length in range(2, len(values)+1)
    for item in combinations(values, length)
    if sum(i[1] for i in item) >= 0.5
]

Related


Python - How to get all possible combinations from a nested list

Hamza Arshad: I have nested lists like this: values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]] How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5? This is what I'm u

Python - How to get all possible combinations from a nested list

Hamza Arshad: I have nested lists like this: values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]] How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5? This is what I'm u

Get all possible combinations of values in a list - Python

Luca F. I have a list ['a', 'bill', 'smith']and I want to write a python code to get all possible combinations of applying a specific criterion. To be more precise, I want to get the combination of these three elements in the list plus the first letter of each

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations of list integers?

cooler I have a list L = [3, 4, 1, 5, 9]and I want to get all possible numbers that can be made by using the numbers present in the list L. I mean, I want to get output like this: [3, 4, 1, 5, 9, 34, . . . . , 91, 953, ... ,41593, ... etc.]I am using the follo

How to get all possible combinations from an array?

username I have an array of Types and I want to generate all possible combinations for these types. ie: (assumes only int, bool and string) int/bool/string/int, bool/int, string/int, bool, string/int, string, bool/etc I saw Eric Lippert's answer here: Generate

get all possible combinations of k elements from a list

Tobias Herman I need a function that does the same thing as itertools.combinations(iterable, r) in python So far I came up with this: {-| forward application -} x -: f = f x infixl 0 -: {-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -} combinatio

Get all possible combinations of k elements from a list

Tobias Herman I need a function that does the same thing as in pythonitertools.combinations(iterable, r) So far I have come up with this: {-| forward application -} x -: f = f x infixl 0 -: {-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -} combin

Get all combinations of a list in Python

CMinusMinus: I want to get all combinations of lists: L = ["a","b","c"] combinations(L,length=2) # [("a","a"),("a","b"),("a","c"),("b","a"),("b","b"),("b","c"),("c","a"),("c","b"),("c","c")] I tried itertools.combinations() but it's back [('a', 'b'), ('a', 'c

Get all combinations of a list in Python

CMinusMinus: I want to get all combinations of lists: L = ["a","b","c"] combinations(L,length=2) # [("a","a"),("a","b"),("a","c"),("b","a"),("b","b"),("b","c"),("c","a"),("c","b"),("c","c")] I tried itertools.combinations() but it's back [('a', 'b'), ('a', 'c

Get all combinations of a list in Python

CMinusMinus: I want to get all combinations of lists: L = ["a","b","c"] combinations(L,length=2) # [("a","a"),("a","b"),("a","c"),("b","a"),("b","b"),("b","c"),("c","a"),("c","b"),("c","c")] I tried itertools.combinations() but it's back [('a', 'b'), ('a', 'c

How to get all possible combinations from two arrays in Java?

Oleg Caralanski: I have two arrays: String[] operators = {"+", "-", "*"}; int[] numbers = {48, 24, 12, 6}; I want all possible combinations of string formats like this: 48+24+12+6 48+24+12-6 48+24+12*6 48+24-12+6 48+24-12-6 48+24-12*6 .......... 48*24*12*6 T