Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input : arr[] = {1, 2, 3} Output : 6 1 + 2 + 3 = 6 Input : arr[] = {15, 12, 13, 10} Output : 50Bàn luận Iterating through the array and adding each element to the sum variable and finally displaying the sum.
Python3
Đưa ra một loạt các số nguyên, tìm tổng các yếu tố của nó.
Phương pháp 1: Lặp lại qua mảng và thêm từng phần tử vào biến tổng và cuối cùng hiển thị tổng.
def Sum of the array is 340
Sum of the array is 341Sum of the array is 342 Sum of the array is 343 Sum of the array is 344
Sum of the array is 341sum(iterable) 7sum(iterable) 8Sum of the array is 342Sum of the array is 340
Sum of the array is 341Sum of the array is 346 Sum of the array is 347Sum of the array is 348 Sum of the array is 349
sum(iterable) 0Sum of the array is 342 Sum of the array is 343 Sum of the array is 342 sum(iterable) 4 sum(iterable) 5
Sum of the array is 341Sum of the array is 343 Sum of the array is 343
Sum of the array is 341Sum of the array is 343 Sum of the array is 346Sum of the array is 347Sum of the array is 348Sum of the array is 349Sum of the array is 348reduce( function, Array );1Sum of the array is 348reduce( function, Array );3reduce( function, Array );4
Sum of the array is 342sum(iterable) 8Sum of the array is 344Sum of the array is 345
Output:
Sum of the array is 34reduce( function, Array );5Sum of the array is 343 reduce( function, Array );7reduce( function, Array );8: O(n), Auxiliary Space: O(1)
reduce( function, Array );9Sum of the array is 343 Sum of the array is 341 Using the built-in function sum(). Python provides an inbuilt function sum() which sums up the numbers in the list.
Syntax:
sum(iterable)Độ phức tạp về thời gian: O (n), không gian phụ trợ: O (1) iterable can be anything list, tuples or dictionaries, but most importantly it should be numbered.
Python3
Sum of the array is 341Sum of the array is 346 Sum of the array is 347Sum of the array is 348 Sum of the array is 349
sum(iterable) 0Sum of the array is 342 Sum of the array is 343 Sum of the array is 342 sum(iterable) 4 sum(iterable) 5
Sum of the array is 341Sum of the array is 343 Sum of the array is 343
Sum of the array is 342sum(iterable) 8Sum of the array is 344Sum of the array is 345
Output:
Sum of the array is 34reduce( function, Array );5Sum of the array is 343 reduce( function, Array );7reduce( function, Array );8: O(n), Auxiliary Space: O(1)
reduce( function, Array );9Sum of the array is 343 Sum of the array is 341 Using the reduce method. Array.reduce() method is used to iterate over the array and get the summarized result from all elements of array.
Syntax:
reduce( function, Array );Độ phức tạp về thời gian: O (n), không gian phụ trợ: O (1)
Phương pháp 2: Sử dụng tổng chức năng tích hợp (). Python cung cấp một tổng số hàm sẵn () tổng hợp các số trong danh sách.
Đưa ra một loạt các số nguyên, tìm tổng các yếu tố của nó.
Phương pháp 1: Lặp lại qua mảng và thêm từng phần tử vào biến tổng và cuối cùng hiển thị tổng.
Sum of the array is 341sum(iterable) 7sum(iterable) 8Sum of the array is 342Sum of the array is 340
Sum of the array is 341Sum of the array is 346 Sum of the array is 347Sum of the array is 348 Sum of the array is 349
sum(iterable) 0Sum of the array is 342 Sum of the array is 343 Sum of the array is 342 sum(iterable) 4 sum(iterable) 5
Sum of the array is 341Sum of the array is 343 Sum of the array is 343
Sum of the array is 341Sum of the array is 343 Sum of the array is 346Sum of the array is 347Sum of the array is 348Sum of the array is 349Sum of the array is 348reduce( function, Array );1Sum of the array is 348reduce( function, Array );3reduce( function, Array );4
Sum of the array is 342sum(iterable) 8Sum of the array is 344Sum of the array is 345
Output:
Sum of the array is 34reduce( function, Array );5Sum of the array is 343 reduce( function, Array );7reduce( function, Array );8
Trong vòng lặp FOR, hãy nối từng số vào danh sách ..
Các mảng Python là một cấu trúc dữ liệu như danh sách, lưu trữ dữ liệu đồng nhất. Mảng lưu trữ các đối tượng của cùng một kiểu dữ liệu. Các mảng có thể thay đổi, có nghĩa là dữ liệu trong một mảng có thể được thay đổi và lặp lại, có nghĩa là mỗi phần tử trong một mảng có thể được truy cập từng cái một.
Trong Python, bạn cần nhập mô -đun mảng để tạo một mảng. Hàm mảng (data_type, value) được sử dụng để chỉ định một mảng với kiểu dữ liệu và danh sách giá trị được đưa ra làm đối số. Nhìn vào ví dụ được đưa ra dưới đây để tạo một mảng trong Python.
import array as ar a = ar.array('i', [1, 3, 5])Ở đây, chúng tôi đã khai báo một mảng các loại số nguyên với danh sách giá trị [1, 3, 5]. Chữ tôi là một mã loại. Điều này xác định loại mảng trong quá trình tạo.
Mã loại thường được sử dụng là
tôi | Số nguyên |
u | unicode |
f | trôi nổi |
l | Số nguyên |
u
unicode arr[] = {15,12,13,10}
f 50
trôi nổi
l
- Chương trình nên lấy đầu vào và đưa ra đầu ra như sau:
- Input- mảng [] = {15,12,13,10}
Đầu ra- 50
Cách tiếp cận để tìm tổng của mảng
Thuật toán
Bước 1- Nhập mô-đun mảng Import array module
Bước 2- Xác định hàm để tính tổng các phần tử trong một mảng- Define a function to calculate the sum of elements in an array
Bước 3- Khai báo một biến để lưu trữ tổngDeclare a variable to store the sum
Bước 4- Tính độ dài của mảng bằng hàm len () Calculate the length of the array using len() function
Bước 5 - Chạy vòng lặp cho tất cả các phần tử trong mảng- Run a loop for all the elements in the array
Bước 6- Thêm từng phần tử vào biến cho tổng- Add each element to the variable for sum one by one
Bước 7- Trả về tổngReturn sum
Bước 8- Khai báo mảng- Declare the array
Bước 9- Hàm gọi- Call function
Bước 10- In kết quả- Print the result
Chương trình Python 1
Nhìn vào chương trình để hiểu việc thực hiện phương pháp nói trên.
import array as ar def SumofArray(arr): sum=0 n = len(arr) for i in range(n): sum = sum + arr[i] return sum #input values to list a = ar.array('i',[10, 21, 12, 13]) # display sum print ('Sum of the array is ', SumofArray(a) )
Tổng của mảng là 56
Cách tiếp cận 2: Sử dụng hàm sum ()
Theo cách tiếp cận này, chúng tôi sẽ sử dụng hàm tích hợp có tên SUM () tính toán tổng của tất cả các phần tử trong một mảng và trả về kết quả.
Thuật toán
Bước 1- Nhập mô-đun mảng- Import array module
Bước 2- Xác định hàm để tính tổng các phần tử trong một mảng- Declare array using the module
Bước 3- Khai báo một biến để lưu trữ tổng- Call sum() function
Bước 4- Tính độ dài của mảng bằng hàm len ()- Print the result
Bước 5 - Chạy vòng lặp cho tất cả các phần tử trong mảng
Nhìn vào chương trình để hiểu việc thực hiện phương pháp nói trên.
import array as arr #input values a = arr.array('i',[10, 21, 12, 13]) # display sum print ('Sum of the array is ', sum(a) )
Tổng của mảng là 56
Cách tiếp cận 2: Sử dụng hàm sum ()
Theo cách tiếp cận này, chúng tôi sẽ sử dụng hàm tích hợp có tên SUM () tính toán tổng của tất cả các phần tử trong một mảng và trả về kết quả.