Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combinaison
#1
Hello,

I'm new in python. I try to generate combinations of objects and calculate sums to characterize them.
A little confused, so I quickly give an example:

I have 3 objects "A", "B", "C" that I want to combine with each other by 2 for example.
In order to achieve this result
[[['A','B'], ['A','C'], ['B','C']]

Now objects have their characteristics
A= {'ST':5, 'SD':7, 'SA':2}
B= {'ST':1, 'SD':3, 'SA':3}
C= {'ST':2, 'SD':2, 'SA':4}

I would like that in addition to obtaining ['A','B'], I have the sum of characteristics of 'A' and 'B'such that[ST=6,SD=10,SA=5] and this for each of the possible combinations.

The expected result would be:
The combination A, B is ST=6,SD=10,SA=5
The combination A, C is ST=7,SD=9,SA=6
The combination B, C is ST=3,SD=5,SA=7

def comb(m, lst):
    if m == 0: return [[]]
    return [[x] + suffix for i, x in enumerate(lst)
            for suffix in comb(m - 1, lst[i + 1:])]
 
seq=['A','B','C']
print(comb(2, seq))
For the moment I can only do the combination....
Thank you in advance
Reply
#2
Check out the itertools library. itertools.combinations(seq, 2) will give you all the combinations of two items in seq.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020