Hướng dẫn write a python program to sum and compute the product of a numpy array elements. - viết một chương trình python để tính tổng và tính tích của một phần tử mảng numpy.

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:50:48 (UTC/GMT +8 giờ)

Numpy: ARRAY Đối tượng Bài tập-99 với giải pháp

Viết một chương trình Numpy để tổng hợp và tính toán sản phẩm của một phần tử mảng Numpy.

Trình bày bằng hình ảnh:

Hướng dẫn write a python program to sum and compute the product of a numpy array elements. - viết một chương trình python để tính tổng và tính tích của một phần tử mảng numpy.

Giải pháp mẫu:-:-

Mã Python:

import numpy as np
x = np.array([10, 20, 30], float)
print("Original array:")
print(x)
print("Sum of the array elements:")
print(x.sum())
print("Product of the array elements:")
print(x.prod())

Đầu ra mẫu:

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 

Trình chỉnh sửa mã Python-Numpy:

Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus.

Trước: Viết một chương trình Numpy để chuyển đổi dữ liệu thô trong một mảng thành chuỗi nhị phân và sau đó tạo một mảng. Write a NumPy program to convert the raw data in an array to a binary string and then create an array.
Next: Write a NumPy program to take values from a source array and put them at specified indices of another array.

Mức độ khó của bài tập này là gì?

Kiểm tra kỹ năng lập trình của bạn với bài kiểm tra của W3Resource.

Python: Lời khuyên trong ngày

Cấu trúc Dict được đặt hàng (một lớp từ điển giữ thứ tự):

>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]


  • Bài tập: Top 16 chủ đề phổ biến nhất hàng tuần
  • Bài tập SQL, Thực hành, Giải pháp - Tham gia
  • Bài tập SQL, Thực hành, Giải pháp - Quan sát phụ
  • JavaScript Basic - Bài tập, Thực hành, Giải pháp
  • Java Array: Bài tập, Thực hành, Giải pháp
  • C Bài tập lập trình, Thực hành, Giải pháp: Tuyên bố có điều kiện
  • Cơ sở dữ liệu nhân sự - Sắp xếp bộ lọc: Bài tập, Thực hành, Giải pháp
  • C Bài tập lập trình, Thực hành, Giải pháp: Chuỗi
  • Các loại dữ liệu Python: Từ điển - Bài tập, Thực hành, Giải pháp
  • Câu đố lập trình Python - Bài tập, Thực hành, Giải pháp
  • Mảng C ++: Bài tập, Thực hành, Giải pháp
  • Báo cáo và vòng lặp có điều kiện JavaScript - Bài tập, Thực hành, Giải pháp
  • Thuật toán cơ bản C# Sharp: Bài tập, Thực hành, Giải pháp
  • Python Lambda - Bài tập, Thực hành, Giải pháp
  • Python Pandas DataFrame: Bài tập, Thực hành, Giải pháp
  • Công cụ chuyển đổi
  • JavaScript: HTML Mẫu xác thực


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. Có thể lặp lại: Có thể là bất cứ thứ gì danh sách, bộ dữ liệu hoặc từ điển, nhưng quan trọng nhất là nó nên được đánh số.

Làm cách nào để tổng hợp hai mảng numpy?

Để thêm hai mảng với nhau, chúng tôi sẽ sử dụng phương thức Numpy.ADD (ARR1, ARR2). Để sử dụng phương pháp này, bạn phải đảm bảo rằng hai mảng có cùng chiều dài. Nếu độ dài của hai mảng không giống nhau, thì hãy phát kích thước của mảng ngắn hơn bằng cách thêm số 0 tại các chỉ mục bổ sung.

Bạn nhận được gì nếu bạn áp dụng Numpy Sum () vào danh sách chỉ chứa các giá trị Boolean?numpy.sum()

Sum nhận được một mảng booleans làm đối số của nó, nó sẽ tổng hợp từng phần tử (đếm đúng là 1 và sai là 0) và trả về kết quả. Ví dụ NP. SUM ([Đúng, Đúng, Sai]) sẽ xuất hiện 2 :) Hy vọng điều này sẽ giúp.numpy.sum(array_name, axis=None, dtype=None, out=None, keepdims=, initial=, where=)

Example:

Python3

import numpy as np

def

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
0

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
4
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
9
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1____22221
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
4
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
5
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
6
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1__28____21__21

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
4

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
7
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
8
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
9
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
7
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
8
Initialised array
[[1 2 3]
 [4 5 6]]
[  6 120]
[ 4 10 18]
720
[720]
[720]
with axis parameter
[[ 4 10 18]]
[[  6]
 [120]]
without axis parameter
[[720]]
using initial parameter in sum function
7200
using where parameter 
[4 1 1]
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Initialised array
[[1 2 3]
 [4 5 6]]
[  6 120]
[ 4 10 18]
720
[720]
[720]
with axis parameter
[[ 4 10 18]]
[[  6]
 [120]]
without axis parameter
[[720]]
using initial parameter in sum function
7200
using where parameter 
[4 1 1]
7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Initialised array
[[1 2 3]
 [4 5 6]]
[  6 120]
[ 4 10 18]
720
[720]
[720]
with axis parameter
[[ 4 10 18]]
[[  6]
 [120]]
without axis parameter
[[720]]
using initial parameter in sum function
7200
using where parameter 
[4 1 1]
9
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
9
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[  1   2   6  24 120 720]
[[  1   2   6]
 [  4  20 120]]
[[1 2 6]
 [4 20 120]]
1

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
7
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
8
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
9
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[  1   2   6  24 120 720]
[[  1   2   6]
 [  4  20 120]]
[[1 2 6]
 [4 20 120]]
9
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8import1

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2import4

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3import8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
7
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
8
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
9
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
9numpy as np7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8numpy as np9
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
7
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
8
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
9
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0numpy as np7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8numpy as np9
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
05
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
7
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
11
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8numpy as np9
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
18
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
7
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
24
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
26
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
31
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
7
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
8
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
9
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
9
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
40
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
42numpy as np9
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
45
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
45
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
48

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
49
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
50
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
53
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
54

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
56

Output:

Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]

Lưu ý: Sử dụng numpy.sum trên các phần tử mảng bao gồm không phải là một số (nans) các phần tử gây ra lỗi, để tránh điều này, chúng tôi sử dụng numpy.nansum () Các tham số tương tự như cái trước ngoại trừ cái sau không hỗ trợ ở đâu và ban đầu.using numpy.sum on array elements consisting Not a Number (NaNs) elements gives an error, To avoid this we use numpy.nansum() the parameters are similar to the former except the latter doesn’t support where and initial.

Phương pháp số 2: Sử dụng numpy.cumsum ()numpy.cumsum()

Trả về tổng tích lũy của các phần tử trong mảng đã cho.

Cú pháp: numpy.cumsum (mảng_name, axis = none, dtype = none, out = none)numpy.cumsum(array_name, axis=None, dtype=None, out=None)

Example:

Python3

import numpy as np

def

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
0

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
4
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
9
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1____22221
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
4
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
5
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
6
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1__28____21__21

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
85
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
4

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
92

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
95
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Initialised array
[[1 2 3]
 [4 5 6]]
[  6 120]
[ 4 10 18]
720
[720]
[720]
with axis parameter
[[ 4 10 18]]
[[  6]
 [120]]
without axis parameter
[[720]]
using initial parameter in sum function
7200
using where parameter 
[4 1 1]
7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
9
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03____21
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
5
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03__

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
16
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[  1   2   6  24 120 720]
[[  1   2   6]
 [  4  20 120]]
[[1 2 6]
 [4 20 120]]
9
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
21

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2import4

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
49
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
50
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
53
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
54

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
56

Output:

Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]

Sản phẩm của mảng numpy

Sản phẩm của các mảng numpy có thể đạt được theo các cách sau & nbsp;

Phương thức số 1: & nbsp; sử dụng numpy.prod ()numpy.prod()

Cú pháp: numpy.prod (Array_Name, Axis = none, dtype = none, out = none, netchims =, ban đầu =, trong đó =)numpy.prod(array_name, axis=None, dtype=None, out=None, keepdims=, initial=, where=)

Example:

Python3

import numpy as np

def

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
0

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
4
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
9
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1____22221
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
4
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
5
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
6
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1__28____21__21

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
4

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Initialised array
[[1 2 3]
 [4 5 6]]
[  6 120]
[ 4 10 18]
720
[720]
[720]
with axis parameter
[[ 4 10 18]]
[[  6]
 [120]]
without axis parameter
[[720]]
using initial parameter in sum function
7200
using where parameter 
[4 1 1]
7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
9
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03____21
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
5
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03__

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
63
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
9
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
75

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2import4

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
63
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[  1   2   6  24 120 720]
[[  1   2   6]
 [  4  20 120]]
[[1 2 6]
 [4 20 120]]
9
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8import1

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2import4

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3import8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
63
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
9numpy as np7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8numpy as np9
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
63
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0numpy as np7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8numpy as np9
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
05
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
23
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8numpy as np9
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
18
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
34
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
36
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
31
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
63
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
9
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
40
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
42numpy as np9
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
45
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
45
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
48

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
49
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
50
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
53
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
54

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
56

Output:

Initialised array
[[1 2 3]
 [4 5 6]]
[  6 120]
[ 4 10 18]
720
[720]
[720]
with axis parameter
[[ 4 10 18]]
[[  6]
 [120]]
without axis parameter
[[720]]
using initial parameter in sum function
7200
using where parameter 
[4 1 1]

Sản phẩm của mảng numpynumpy.cumprod()

Sản phẩm của các mảng numpy có thể đạt được theo các cách sau & nbsp;

Phương thức số 1: & nbsp; sử dụng numpy.prod ()numpy.cumsum(array_name, axis=None, dtype=None, out=None)axis = [integer,Optional]

Python3

import numpy as np

def

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
0

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
4
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
9
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1____22221
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
4
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
5
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
6
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1__28____21__21

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
3
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
85
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
5

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
[[1 2 3]
 [4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
 [15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter 
[5 0 0]
4

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
00

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
03
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
2

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Initialised array
[[1 2 3]
 [4 5 6]]
[  6 120]
[ 4 10 18]
720
[720]
[720]
with axis parameter
[[ 4 10 18]]
[[  6]
 [120]]
without axis parameter
[[720]]
using initial parameter in sum function
7200
using where parameter 
[4 1 1]
7
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
9
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
1
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03____21
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
5
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
03__

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[ 1  3  6 10 15 21]
[[ 1  3  6]
 [ 4  9 15]]
[[1 3 6]
 [4 9 15]]
24
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
0
Initialised array
original array
[[1 2 3]
 [4 5 6]]
[  1   2   6  24 120 720]
[[  1   2   6]
 [  4  20 120]]
[[1 2 6]
 [4 20 120]]
9
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]
21

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
2import4

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
49
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
50
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
8
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
53
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
54

Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
1
Original array:                                                                   
[ 10.  20.  30.]                                                                
Sum of the array elements:                                                          
60.0                                                                       
Product of the array elements:                                                      
6000.0 
56

Output:

Initialised array
original array
[[1 2 3]
 [4 5 6]]
[  1   2   6  24 120 720]
[[  1   2   6]
 [  4  20 120]]
[[1 2 6]
 [4 20 120]]

Làm thế nào để bạn tính tổng của tất cả các phần tử trong một mảng numpy?

SUM () trong Python. numpy.sum (mảng, trục, dtype, out): hàm này trả về tổng các phần tử mảng trên trục được chỉ định.numpy. sum(arr, axis, dtype, out) : This function returns the sum of array elements over the specified axis.

Làm thế nào để bạn tổng hợp một mảng các phần tử trong Python?

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.Có thể lặp lại: Có thể là bất cứ thứ gì danh sách, bộ dữ liệu hoặc từ điển, nhưng quan trọng nhất là nó nên được đánh số.Using the built-in function sum(). Python provides an inbuilt function sum() which sums up the numbers in the list. iterable: iterable can be anything list, tuples or dictionaries, but most importantly it should be numbered.

Làm cách nào để tổng hợp hai mảng numpy?

Để thêm hai mảng với nhau, chúng tôi sẽ sử dụng phương thức Numpy.ADD (ARR1, ARR2).Để sử dụng phương pháp này, bạn phải đảm bảo rằng hai mảng có cùng chiều dài.Nếu độ dài của hai mảng không giống nhau, thì hãy phát kích thước của mảng ngắn hơn bằng cách thêm số 0 tại các chỉ mục bổ sung.numpy. add(arr1,arr2) method. In order to use this method, you have to make sure that the two arrays have the same length. If the lengths of the two arrays are​ not the same, then broadcast the size of the shorter array by adding zero's at extra indexes.

Bạn nhận được gì nếu bạn áp dụng Numpy Sum () vào danh sách chỉ chứa các giá trị Boolean?

Sum nhận được một mảng booleans làm đối số của nó, nó sẽ tổng hợp từng phần tử (đếm đúng là 1 và sai là 0) và trả về kết quả.Ví dụ NP.SUM ([Đúng, Đúng, Sai]) sẽ xuất hiện 2 :) Hy vọng điều này sẽ giúp.sum each element (count True as 1 and False as 0) and return the outcome. for instance np. sum([True, True, False]) will output 2 :) Hope this helps.