Posts: 5 Threads: 1 Joined: Oct 2018 Oct-22-2018, 07:55 PM (This post was last modified: Oct-22-2018, 07:55 PM by mattis.) I would like to count sum(subsets) in the following code, how can i do this? thanks. import itertools thelist = [0, 0, 0, 1, 1, 2, 2, 1] for L in range(5,6): for subset in itertools.combinations(thelist, L): print(f"{subset} => {sum(subset)}") For example, I would like to count where sum(subset)=6,7 etc. Posts: 12,117 Threads: 494 Joined: Sep 2016 please clarify: count increment condition(s) --> ? it's not clear as written Posts: 5 Threads: 1 Joined: Oct 2018 Oct-22-2018, 08:21 PM (This post was last modified: Oct-22-2018, 08:22 PM by mattis.) When I run this script, I get a result that looks like this: (0, 0, 0, 1, 1) => 2 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 1) => 2 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 1) => 2 (0, 0, 0, 2, 2) => 4 (0, 0, 0, 2, 1) => 3 (0, 0, 0, 2, 1) => 3 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 1) => 3 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 2, 2, 1) => 5 (0, 0, 1, 1, 2) => 4 On the right is the sums of subsets which are in parenthesis. I'd like to count numbers of say 4s, 5s, 6s etc and printed at the bottom. How can I achieve this? Posts: 3,458 Threads: 101 Joined: Sep 2016 Counter from the collections module can handle this easily enough: >>> from collections import Counter >>> totals = Counter() >>> items = [0, 0, 0, 1, 1, 2, 2, 1] >>> for L in range(5, 6): ... for subs in itertools.combinations(items, L): ... totals[sum(subs)] += 1 ... >>> totals Counter({4: 19, 5: 15, 3: 9, 6: 9, 2: 3, 7: 1}) Posts: 5 Threads: 1 Joined: Oct 2018 Oct-22-2018, 08:36 PM (This post was last modified: Oct-22-2018, 08:39 PM by mattis.) thank you, Nilamo. I changed it like this with your suggestion. but I couldn't get the result. I am a new learner, what am I doing wrong here? please import itertools from collections import Counter thelist = [0, 0, 0, 1, 1, 2] for L in range(5,6): for subset in itertools.combinations(thelist, L): print(f"{subset} => {sum(subset)}") totals = Counter() totals[sum(subset)] += 1 totals Posts: 3,458 Threads: 101 Joined: Sep 2016 Create the counter before the loop. Increment the counter inside the loop. Print the counter after the loop. Posts: 5 Threads: 1 Joined: Oct 2018 Oct-22-2018, 08:44 PM (This post was last modified: Oct-22-2018, 08:45 PM by mattis.) Thanks a lot! I got it now. Here is the final one: import itertools from collections import Counter totals = Counter() thelist = [0, 0, 0, 1, 1, 2, 2, 2] for L in range(5,6): for subset in itertools.combinations(thelist, L): print(f"{subset} => {sum(subset)}") totals[sum(subset)] += 1 totals Posts: 12,117 Threads: 494 Joined: Sep 2016 use a list: import itertools max_digit = 9 # initialize counts list to 0 counts = [0] * (max_digit +1) thelist = [0, 0, 0, 1, 1, 2, 2, 1] for L in range(5,6): for subset in itertools.combinations(thelist, L): sum_subset = sum(subset) print(f"{subset} => {sum_subset}") counts[sum_subset] += 1 print('\nCount of sums:') for n in range(max_digit + 1): print(f"sum of {n}'s' is {counts[n]}")result: Output: (0, 0, 0, 1, 1) => 2 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 1) => 2 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 1) => 2 (0, 0, 0, 2, 2) => 4 (0, 0, 0, 2, 1) => 3 (0, 0, 0, 2, 1) => 3 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 1) => 3 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 2, 2, 1) => 5 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 1) => 3 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 2, 2, 1) => 5 (0, 1, 1, 2, 2) => 6 (0, 1, 1, 2, 1) => 5 (0, 1, 1, 2, 1) => 5 (0, 1, 2, 2, 1) => 6 (0, 1, 2, 2, 1) => 6 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 1) => 3 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 2, 2, 1) => 5 (0, 1, 1, 2, 2) => 6 (0, 1, 1, 2, 1) => 5 (0, 1, 1, 2, 1) => 5 (0, 1, 2, 2, 1) => 6 (0, 1, 2, 2, 1) => 6 (0, 1, 1, 2, 2) => 6 (0, 1, 1, 2, 1) => 5 (0, 1, 1, 2, 1) => 5 (0, 1, 2, 2, 1) => 6 (0, 1, 2, 2, 1) => 6 (1, 1, 2, 2, 1) => 7 Count of sums: sum of 0's' is 0 sum of 1's' is 0 sum of 2's' is 3 sum of 3's' is 9 sum of 4's' is 19 sum of 5's' is 15 sum of 6's' is 9 sum of 7's' is 1 sum of 8's' is 0 sum of 9's' is 0 Posts: 12,117 Threads: 494 Joined: Sep 2016 Posts: 5 Threads: 1 Joined: Oct 2018 Thank you, Larz60+. I will examine your code too. |