>>> phonelist = [[12,14,16,17,18],[16,23,54,64,32]]
>>> [sum(li) for li in phonelist]
[77, 189]
>>> sum([sum(li) for li in phonelist])
266
or:
>>> sum(sum(li) for li in phonelist) # generator expression...
266
If you are trying to create individual categories, you can use a dict:
data={'Bar {}'.format(i):sum(li) for i, li in enumerate(phonelist, 1)}
data['Total']=sum(data.values())
print data
{'Bar 2': 189, 'Bar 1': 77, 'Total': 266}
Then if you want to produce a simple bar graph:
for bar in sorted(data.keys()):
print '{}: {}'.format(bar, int(round(25.0*data[bar]/data['Total']))*'*')
Prints:
Bar 1: *******
Bar 2: ******************
Total: *************************
Answer from dawg on Stack Overflow>>> phonelist = [[12,14,16,17,18],[16,23,54,64,32]]
>>> [sum(li) for li in phonelist]
[77, 189]
>>> sum([sum(li) for li in phonelist])
266
or:
>>> sum(sum(li) for li in phonelist) # generator expression...
266
If you are trying to create individual categories, you can use a dict:
data={'Bar {}'.format(i):sum(li) for i, li in enumerate(phonelist, 1)}
data['Total']=sum(data.values())
print data
{'Bar 2': 189, 'Bar 1': 77, 'Total': 266}
Then if you want to produce a simple bar graph:
for bar in sorted(data.keys()):
print '{}: {}'.format(bar, int(round(25.0*data[bar]/data['Total']))*'*')
Prints:
Bar 1: *******
Bar 2: ******************
Total: *************************
len(phonelist[0]) is an int, so you can't loop over it. You can change that to
for x in phonelist[0]:
This way x will take on each value of phonelist[0].
How can I find the sum of the numbers in a list for a 'for' loop?
Given a list of numbers, calculate their sum using a for loop. Output the sum after the loop
How can i sum the value in list with for loop in python? - Stack Overflow
How to calculate min/max values in a list without using the min()/max() functions?
I'm a complete beginner. I've been following a course through Solo Learn, and it's been okay, but the latest lesson was really poorly explained. (It wasn't explained at all. I had to rely on comments in the lesson to actually learn things. Really rough.)
The lesson is ending in a practice exercise with the following directions:
Given a list of numbers, calculate their sum using a for loop.
There's just one problem! They haven't taught us how to add anything in a for loop. After some digging on Google and Stack Overflow, I learned a few things, but I still haven't gotten the correct answer. I keep just getting wrong answers.
This is what my current code looks like. I'm at a loss and don't know what to do at this point. The list is provided by the course and I can't change it. Other than the first line, I wrote everything else.
The expected outcome is: 9803
list = [42, 8, 7, 1, 0, 124, 8897, 555, 3, 67, 99]
sum = 0
for x in list:
sum = sum + x
print(sum)The outcome to this problem with my current code is: 42 50 57 58 58 182 9079 9634 9637 9704 9803
I have no idea why this happens. I just need that bottom number, but I don't know how to get it.
I originally tried doing something like
for x in list:
sum += x
print(sum)But that didn't work either.
Any guidance would be really appreciated. I'm sorry if this is a simple solution, but they didn't teach us how to do this at all. Instead, they focused on modulus operations (%) for the lesson. That was really poorly explained too.
I found a similar question on Stack Overflow here. But it wasn't very helpful, and the answers use things that I haven't learned yet.
I don't know. Maybe I'm not smart. I'm not sure what to do now.
Any help is appreciated, thank you.
It seems that you are wanting the sum of squares of the elements of ds, although your code is trying to index ds using one of its values (i), and is also trying to compute the sum inside the loop.
You can do it in one line using:
print(sum([i*i for i in ds]))
or
print(sum(i*i for i in ds))
(These two similar looking expressions do different things under the hood, although I'll skip a discussion of this for now.)
Or you could build up the sum e.g. by doing:
total = 0
for i in ds:
total += i * i
print(total)
or use a combination of the two approaches:
squares = []
for i in ds:
squares.append(i * i)
print(sum(squares))
Try this:
ds = [1,2,3,4,5]
total = 0
for i in ds:
total += i * i
print(total)
The output is 55