Count of sums - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Count of sums (/thread-13597.html) |
Count of sums - mattis - Oct-22-2018 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. RE: Count of sums - Larz60+ - Oct-22-2018 please clarify: count increment condition(s) --> ? it's not clear as written RE: Count of sums - mattis - Oct-22-2018 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? RE: Count of sums - nilamo - Oct-22-2018 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}) RE: Count of sums - mattis - Oct-22-2018 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 RE: Count of sums - nilamo - Oct-22-2018 Create the counter before the loop. Increment the counter inside the loop. Print the counter after the loop. RE: Count of sums - mattis - Oct-22-2018 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 RE: Count of sums - Larz60+ - Oct-22-2018 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:
RE: Count of sums - Larz60+ - Oct-22-2018 dueling posts RE: Count of sums - mattis - Oct-22-2018 Thank you, Larz60+. I will examine your code too. |