Python массивы сумма элементов

Sum a list of numbers in Python [duplicate]

So you want (element 0 + element 1) / 2, (element 1 + element 2) / 2, . etc.

We make two lists: one of every element except the first, and one of every element except the last. Then the averages we want are the averages of each pair taken from the two lists. We use zip to take pairs from two lists.

I assume you want to see decimals in the result, even though your input values are integers. By default, Python does integer division: it discards the remainder. To divide things through all the way, we need to use floating-point numbers. Fortunately, dividing an int by a float will produce a float, so we just use 2.0 for our divisor instead of 2 .

averages = [(x + y) / 2.0 for (x, y) in zip(my_list[:-1], my_list[1:])] 

Since zip stops once it reaches the end of the shorter argument, zip(my_list, my_list[1:]) is sufficient.

Yes, and that’s how I usually see it written; stylistically I prefer the symmetry of slicing both, although it’s less efficient.

I swapped the order of the sub-questions to make this more useful for the most common case that most beginners are searching for. Also, the question/top answer have 2 million views but very few upvotes in comparison to other popular questions (that have 10x the upvotes!). Perhaps this is an indication that the less common «sub-question» should be removed altogether. Do you agree with this?

@MateenUlhaq please take this issue to Meta. My first reaction is that I agree, but this needs to be discussed.

@MateenUlhaq even a very low scoring, unpopular question is better to use as the canonical if it meets standards while this one does not (it is clearly two completely unrelated questions, and thus «needs more focus»). The best I’ve found specifically for the sum question so far (i.e., without introducing any other weird requirements such as converting strings to integers — i.e. stackoverflow.com/questions/11344827/summing-elements-in-a-list is not good enough) is stackoverflow.com/questions/17472771/…; but I will keep looking.

Generate a new list with adjacent elements averaged in xs using a list comprehension:

[(x + y) / 2 for x, y in zip(xs, xs[1:])] 

Sum all those adjacent elements into a single value:

sum((x + y) / 2 for x, y in zip(xs, xs[1:])) 

I think he wants to sum adjacent elements. There would be no point in taking the average of x and x — 1 ; we could just subtract 0.5 instead.

Question 2: To sum a list of integers:

a = [2, 3, 5, 8] sum(a) # 18 # or you can do: sum(i for i in a) # 18 

If the list contains integers as strings:

a = ['5', '6'] # import Decimal: from decimal import Decimal sum(Decimal(i) for i in a) 
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sm = sum(a[0:len(a)]) # Sum of 'a' from 0 index to 9 index. sum(a) == sum(a[0:len(a)] print(sm) # Python 3 print sm # Python 2 

no need to create a copy like this, and it’s horribly unpythonic. Avoid like the plague despite all the votes.

for starters a[0:len(a)] creates a copy of a , what’s the point besides wasting CPU & memory? then print(sm) also works in python 2. I don’t understand why this has so many upvotes in mid-2017. but it applies to most of the answers here.

>>> a = range(10) >>> sum(a) Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not callable >>> del sum >>> sum(a) 45 

It seems that sum has been defined in the code somewhere and overwrites the default function. So I deleted it and the problem was solved.

Читайте также:  Css таблица не помещается

This question has been answered here

Using a simple list-comprehension and the sum :

>> sum(i for i in range(x))/2. #if x = 10 the result will be 22.5 

You don’t need to use [ and ] , you can just pass the generator expression sum(i/2. for i in range(x))

All answers did show a programmatic and general approach. I suggest a mathematical approach specific for your case. It can be faster in particular for long lists. It works because your list is a list of natural numbers up to n :

Let’s assume we have the natural numbers 1, 2, 3, . 10 :

>>> nat_seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

You can use the sum function on a list:

You can also use the formula n*(n+1)/2 where n is the value of the last element in the list (here: nat_seq[-1] ), so you avoid iterating over elements:

>>> print (nat_seq[-1]*(nat_seq[-1]+1))/2 55 

To generate the sequence (1+2)/2, (2+3)/2, . (9+10)/2 you can use a generator and the formula (2*k-1)/2. (note the dot to make the values floating points). You have to skip the first element when generating the new list:

>>> new_seq = [(2*k-1)/2. for k in nat_seq[1:]] >>> print new_seq [1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5] 

Here too, you can use the sum function on that list:

But you can also use the formula (((n*2+1)/2)**2-1)/2 , so you can avoid iterating over elements:

The simplest way to solve this problem:

l =[1,2,3,4,5] sum=0 for element in l: sum+=element print sum 

So many solutions, but my favourite is still missing:

>>> import numpy as np >>> arr = np.array([1,2,3,4,5]) 

a numpy array is not too different from a list (in this use case), except that you can treat arrays like numbers:

>>> ( arr[:-1] + arr[1:] ) / 2.0 [ 1.5 2.5 3.5 4.5] 

explanation

The fancy indices mean this: [1:] includes all elements from 1 to the end (thus omitting element 0), and [:-1] are all elements except the last one:

>>> arr[:-1] array([1, 2, 3, 4]) >>> arr[1:] array([2, 3, 4, 5]) 

So adding those two gives you an array consisting of elements (1+2), (2+3) and so on. Note that I’m dividing by 2.0 , not 2 because otherwise Python believes that you’re only using integers and produces rounded integer results.

advantage of using numpy

Numpy can be much faster than loops around lists of numbers. Depending on how big your list is, several orders of magnitude faster. Also, it’s a lot less code, and at least to me, it’s easier to read. I’m trying to make a habit out of using numpy for all groups of numbers, and it is a huge improvement to all the loops and loops-within-loops I would otherwise have had to write.

import itertools def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), . " a, b = itertools.tee(iterable) next(b, None) return itertools.izip(a, b) def pair_averages(seq): return ( (a+b)/2 for a, b in pairwise(seq) ) 
import numpy as np x = [1,2,3,4,5] [(np.mean((x[i],x[i+1]))) for i in range(len(x)-1)] # [1.5, 2.5, 3.5, 4.5] 

Generators are an easy way to write this:

from __future__ import division # ^- so that 3/2 is 1.5 not 1 def averages( lst ): it = iter(lst) # Get a iterator over the list first = next(it) for item in it: yield (first+item)/2 first = item print list(averages(range(1,11))) # [1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5] 

Let us make it easy for Beginner:-

  1. The global keyword will allow the global variable message to be assigned within the main function without producing a new local variable
 message = "This is a global!" def main(): global message message = "This is a local" print(message) main() # outputs "This is a local" - From the Function call print(message) # outputs "This is a local" - From the Outer scope 

This concept is called Shadowing

nums = [1, 2, 3, 4, 5] var = 0 def sums(): for num in nums: global var var = var + num print(var) if __name__ == '__main__': sums() 

Outputs = 15

Читайте также:  Android java socket connect

In Python 3.8, the new assignment operator can be used

>>> my_list = [1, 2, 3, 4, 5] >>> itr = iter(my_list) >>> a = next(itr) >>> [(a + (a:=x))/2 for x in itr] [1.5, 2.5, 3.5, 4.5] 

a is a running reference to the previous value in the list, hence it is initialized to the first element of the list and the iteration occurs over the rest of the list, updating a after it is used in each iteration.

An explicit iterator is used to avoid needing to create a copy of the list using my_list[1:] .

def ave(x,y): return (x + y) / 2.0 map(ave, a[:-1], a[1:]) 
>>> a = range(10) >>> map(ave, a[:-1], a[1:]) [0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5] 

Due to some stupidity in how Python handles a map over two lists, you do have to truncate the list, a[:-1] . It works more as you’d expect if you use itertools.imap :

>>> import itertools >>> itertools.imap(ave, a, a[1:]) >>> list(_) [0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5] 

Short, yes. Simple? It requires an explanation longer than the long solutions to understand what it’s doing.

@Jean-FrançoisFabre Both methods are imperfect — dividing at the end will overflow for large numbers, the solution depends on the data (and the use case).

You can also do the same using recursion:

Python Snippet:

def sumOfArray(arr, startIndex): size = len(arr) if size == startIndex: # To Check empty list return 0 elif startIndex == (size - 1): # To Check Last Value return arr[startIndex] else: return arr[startIndex] + sumOfArray(arr, startIndex + 1) print(sumOfArray([1,2,3,4,5], 0)) 

In the spirit of itertools. Inspiration from the pairwise recipe.

from itertools import tee, izip def average(iterable): "s -> (s0,s1)/2.0, (s1,s2)/2.0, . " a, b = tee(iterable) next(b, None) return ((x+y)/2.0 for x, y in izip(a, b)) 
>>>list(average([1,2,3,4,5])) [1.5, 2.5, 3.5, 4.5] >>>list(average([1,20,31,45,56,0,0])) [10.5, 25.5, 38.0, 50.5, 28.0, 0.0] >>>list(average(average([1,2,3,4,5]))) [2.0, 3.0, 4.0] 

I use a while loop to get the result:

Loop through elements in the list and update the total like this:

def sum(a): total = 0 index = 0 while index < len(a): total = total + a[index] index = index + 1 return total 

Thanks to Karl Knechtel i was able to understand your question. My interpretation:

  1. You want a new list with the average of the element i and i+1.
  2. You want to sum each element in the list.
Читайте также:  Window event code javascript

First question using anonymous function (aka. Lambda function):

s = lambda l: [(l[0]+l[1])/2.] + s(l[1:]) if len(l)>1 else [] #assuming you want result as float s = lambda l: [(l[0]+l[1])//2] + s(l[1:]) if len(l)>1 else [] #assuming you want floor result 

Second question also using anonymous function (aka. Lambda function):

p = lambda l: l[0] + p(l[1:]) if l!=[] else 0 

Both questions combined in a single line of code :

s = lambda l: (l[0]+l[1])/2. + s(l[1:]) if len(l)>1 else 0 #assuming you want result as float s = lambda l: (l[0]+l[1])/2. + s(l[1:]) if len(l)>1 else 0 #assuming you want floor result 

use the one that fits best your needs

Try using a list comprehension. Something like:

new_list = [(old_list[i] + old_list[i+1])/2 for i in range(len(old_list-1))] 

@Rafe it's a working one (if we just fix the parentheses at the end - should be range(len(old_list) - 1) ), but Pythonistas generally frown upon the combination of 'range' and 'len'. A corollary to "there should only be one way to do it" is "the standard library provides a way for you to avoid ugly things". Indirect iteration - iterating over a sequence of numbers, so that you can use those numbers to index what you really want to iterate over - is an ugly thing.

I'd just use a lambda with map()

a = [1,2,3,4,5,6,7,8,9,10] b = map(lambda x, y: (x+y)/2.0, fib[:-1], fib[1:]) print b 
n = int(input("Enter the length of array: ")) list1 = [] for i in range(n): list1.append(int(input("Enter numbers: "))) print("User inputs are", list1) list2 = [] for j in range(0, n-1): list2.append((list1[j]+list1[j+1])/2) print("result mt24"> 
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f4.0%2f" data-se-share-sheet-license-name="CC BY-SA 4.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this answer
)">edited Jun 2, 2018 at 20:46
Peter Mortensen
30.8k 21 gold badges 105 silver badges 131 bronze badges
answered Aug 12, 2015 at 12:17
Add a comment |
0

A simple way is to use the iter_tools permutation

# If you are given a list numList = [1,2,3,4,5,6,7] # and you are asked to find the number of three sums that add to a particular number target = 10 # How you could come up with the answer? from itertools import permutations good_permutations = [] for p in permutations(numList, 3): if sum(p) == target: good_permutations.append(p) print(good_permutations) 
[(1, 2, 7), (1, 3, 6), (1, 4, 5), (1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 1, 7), (2, 3, 5), (2, 5, 3), (2, 7, 1), (3, 1, 6), (3, 2, 5), (3, 5, 2), (3, 6, 1), (4, 1, 5), (4, 5, 1), (5, 1, 4), (5, 2, 3), (5, 3, 2), (5, 4, 1), (6, 1, 3), (6, 3, 1), (7, 1, 2), (7, 2, 1)] 

Note that order matters - meaning 1, 2, 7 is also shown as 2, 1, 7 and 7, 1, 2. You can reduce this by using a set.

Источник

Оцените статью