Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count of sums
#1
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.
Reply
#2
please clarify:
count increment condition(s) --> ?
it's not clear as written
Reply
#3
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?
Reply
#4
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})
Reply
#5
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
Reply
#6
Create the counter before the loop.
Increment the counter inside the loop.
Print the counter after the loop.
Reply
#7
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
Reply
#8
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
Reply
#9
dueling posts
Reply
#10
Thank you, Larz60+. I will examine your code too.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,267 Oct-18-2022, 03:52 AM
Last Post: Yegor123

Forum Jump:

User Panel Messages

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