code for computing and printing list of combinations

Peter Otten __peter__ at web.de
Tue Mar 20 11:20:32 EDT 2012


Joi Mond wrote:

> Can someone help me with the proper code to compute combinations for n=7,
> r=5 for the following list of numbers: 7, 8, 10, 29, 41, 48, 55. There
> should be 21 combination. Also once there list is made can a code be
> written to add (sum) each of the set of five number in the the list. For
> example list: 8, 10, 29, 48, 55, = 150. Thanks

>>> [sum(x) for x in itertools.combinations([7,8,10,29,41,48,55], 5)]
[95, 102, 109, 114, 121, 128, 133, 140, 147, 159, 135, 142, 149, 161, 180, 
136, 143, 150, 162, 181, 183]

Best wishes to your teacher...




More information about the Python-list mailing list