Hướng dẫn what does accumulate () do in python? - Tích lũy () làm gì trong python?

Mô -đun Python Itertools là một tập hợp các công cụ để xử lý các trình lặp lại.

Theo tài liệu chính thức:

Mô -đun [mà] thực hiện một số khối xây dựng vòng lặp được lấy cảm hứng từ các cấu trúc từ APL, Haskell và SML, chúng tạo thành một đại số iterator, giúp xây dựng các công cụ chuyên dụng một cách ngắn gọn và hiệu quả trong python thuần túy. Điều này về cơ bản có nghĩa là các chức năng trong itertools hoạt động trên các trình lặp để tạo ra các trình lặp phức tạp hơn.

Nói một cách đơn giản, trình lặp là các loại dữ liệu có thể được sử dụng trong một vòng lặp. Công cụ lặp lại phổ biến nhất trong Python là danh sách.

Hãy để tạo ra một danh sách các chuỗi và được đặt tên là màu sắc. Chúng ta có thể sử dụng một vòng lặp để lặp lại danh sách như:

Các

import itertools
import operator ## only needed if want to play with operators
5
import itertools
import operator ## only needed if want to play with operators
6
import itertools
import operator ## only needed if want to play with operators
7
import itertools
import operator ## only needed if want to play with operators
8

import itertools
import operator ## only needed if want to play with operators
9
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
0
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
1

Output:

red
orange
yellow
green

Có nhiều loại lặp khác nhau nhưng hiện tại, chúng tôi sẽ sử dụng danh sách và bộ.

Yêu cầu làm việc với itertools

Phải nhập mô -đun ITERTOOLS trước khi sử dụng. Chúng tôi cũng phải nhập mô -đun toán tử vì chúng tôi muốn làm việc với các nhà khai thác.itertools module before using. We have to also import the operator module because we want to work with operators.

import itertools
import operator ## only needed if want to play with operators

Mô -đun ITERTOOLS là một tập hợp các chức năng. Chúng tôi sẽ khám phá một trong các hàm tích lũy () này. module is a collection of functions. We are going to explore one of these accumulate() function.

Lưu ý: Để biết thêm thông tin, hãy tham khảo Python Itertools For more information, refer to Python Itertools

accumulate()

Trình lặp này có hai đối số, mục tiêu có thể lặp lại và hàm sẽ được theo dõi tại mỗi lần lặp có giá trị trong mục tiêu. Nếu không có chức năng nào được thông qua, bổ sung diễn ra theo mặc định. Nếu đầu vào có thể trống, đầu ra có thể lặp lại cũng sẽ trống.

Cú pháp
itertools.accumulate(iterable[, func]) –> accumulate object

Hàm này tạo ra một trình lặp trả về kết quả của một hàm.

Tham sốIderable & Function
iterable & function

Bây giờ nó đủ của phần lý thuyết cho phép chơi với mã

Mã: 1

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
2
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
3

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
2
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
5

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
6= [
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
9_______

5
max(5, 3)
5
max(5, 6)
6
max(6, 2)
6
max(6, 1)
6
max(6, 9)
9
max(9, 1)
9
9= colors 1

colors 2colors 3

import itertools
import operator ## only needed if want to play with operators
5
import itertools
import operator ## only needed if want to play with operators
6
import itertools
import operator ## only needed if want to play with operators
7 colors 7

import itertools
import operator ## only needed if want to play with operators
9
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
0
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
1

Giải trình :

Người vận hành.mul lấy hai số và nhân chúng.

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120

Bây giờ trong ví dụ tiếp theo, chúng ta sẽ sử dụng hàm tối đa vì nó cũng có chức năng làm tham số.

Mã 2:

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
2
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
3

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
2
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
5

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
6= [
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
9_______

5
max(5, 3)
5
max(5, 6)
6
max(6, 2)
6
max(6, 1)
6
max(6, 9)
9
max(9, 1)
9
9= colors 1

import itertools
import operator ## only needed if want to play with operators
5
import itertools
import operator ## only needed if want to play with operators
6
import itertools
import operator ## only needed if want to play with operators
7 colors 7

import itertools
import operator ## only needed if want to play with operators
9
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
0
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
1

Explanation:

5
max(5, 3)
5
max(5, 6)
6
max(6, 2)
6
max(6, 1)
6
max(6, 9)
9
max(9, 1)
9

Giải trình :The passing function is optional as if you will not pass any function items will be summed i.e. added by default.

Người vận hành.mul lấy hai số và nhân chúng.
This return accumulate of items of difference between sets.

Bây giờ trong ví dụ tiếp theo, chúng ta sẽ sử dụng hàm tối đa vì nó cũng có chức năng làm tham số.

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
2
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
3

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
2
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
5

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
6= [
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
9_______

5
max(5, 3)
5
max(5, 6)
6
max(6, 2)
6
max(6, 1)
6
max(6, 9)
9
max(9, 1)
9
9= colors 1

import itertools
import operator ## only needed if want to play with operators
5
import itertools
import operator ## only needed if want to play with operators
6
import itertools
import operator ## only needed if want to play with operators
7 colors 7

import itertools
import operator ## only needed if want to play with operators
9
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
0
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
1