Home python The sum of the entered array elements

The sum of the entered array elements

Author

Date

Category

How to find the sum of all elements of a one-dimensional array, the values ​​of which entered the user in python 3.x?


Answer 1, Authority 100%

Using the SUM ()

function

Input function accepts an icing object, for example, the list

list

User can enter characters in one line and then this line must be divided into words, words to convert to the list of numbers and calculate the sum of these numbers

Sample code:

Print (SUM (MAP (Int, Input (). Split ())))

User can enter sequentially one after another N numbers and then it is necessary all that it is inserted to arrange in the form of a list and calculate the amount on this list

Sample code:

print (SUM (INT (INPUT ()) FOR _ IN RANGE (N)))

Answer 2, Authority 71%

There are different ways, but the proposed Zhihar The fastest. I was confused for myself, viewed at the time of execution. Fill out a list of 1,000,000 elements by random values ​​in the range from 2 ^ 64 to 2 ^ 65, and then run through 4 different methods 100 times:

edited ruled the length of the array for the cycles on the tip Crazyelf (Special thanks), While has been rehabilitated a bit, but the position in the ranking did not change and added numpy;)

edited 2 Thanks for the comment extrn , real speed increase:

import operator
Import Time.
Import NUMPY AS NP
from Functools Import Reduce
From Random Import Randint
LST = [Randint (2 ** 64, 2 ** 65) for _ in range (1000000)]
LENGTH = LEN (LST)
NP_LST = NP.Array (LST)
Def Time_Decorator (Function):
  Def Accepting_Args (* Args):
    start_time = time.monotonic ()
    For i in Range (100):
      Function (* Args)
    Print (F "--- {Function .__ Name__} Takes {(Time.monotonic () - start_time)} Seconds ---")
  Return Accepting_Args.
@Time_Decorator
DEF SUM_LIST (DATA):
  RETURN SUM (DATA)
@Time_Decorator
DEF FOR_LIST (DATA):
  Total = 0.
  FOR ELE IN DATA:
    Total + = Ele
  Return Total
@Time_Decorator
DEF Reduce_List (DATA):
  Return Reduce (Operator.add, Data)
@Time_Decorator
Def While_List (Data, Length):
  Total = 0.
  Ele = 0.
  While Ele & LT; Length:
    Total = Total + Data [ELE]
    ELE + = 1
  Return Total
@Time_Decorator
DEF NP_ARRAY (NP_LST):
  RETURN NP.SUM (NP_LST)
SUM_LIST (LST)
FOR_LIST (LST)
Reduce_List (LST)
While_List (LST, Length)
NP_ARRAY (NP_LST)

The results of old measurements:

--- Sum_List Takes 2.1900558471679688 Seconds ---
--- FOR_LIST TAKES 5.327928066253662 SECONDS ---
--- REDUCE_LIST TAKES 9.168933391571045 SECONDS ---
--- WHILE_LIST TAKES 9.949931144714355 SECONDS ---
--- NP_ARRAY TAKES 0.0734705924987793 Seconds ---

Results After the changes proposed extrn :

--- Sum_List Takes 2.176893711090088 Seconds
--- FOR_LIST TAKES 3.3837478160858154 SECONDS ---
--- REDUCE_LIST TAKES 3.482226610183716 SECONDS ---
--- WHILE_LIST TAKES 10.27085542678833 SECONDS ---
--- NP_ARRAY TAKES 2.422048568725586 SECONDS ---

i.e. Obviously, the use of Sum the fastest option (somewhere near NUMPY ). In second place, bust in the for cycle, on the third use of reduce , and, suddenly :), the most “sad” cycle While

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions