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:
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 operators5 import itertools import operator ## only needed if want to play with operators6 import itertools import operator ## only needed if want to play with operators7 import itertools import operator ## only needed if want to play with operators8 import itertools import operator ## only needed if want to play with operators9 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1200 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1201 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.
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 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) 1202 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1203 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1202 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1205 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1206 = [ operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1209_______ 5 max(5, 3) 5 max(5, 6) 6 max(6, 2) 6 max(6, 1) 6 max(6, 9) 9 max(9, 1) 99 = colors 1
import itertools import operator ## only needed if want to play with operators5 import itertools import operator ## only needed if want to play with operators6 import itertools import operator ## only needed if want to play with operators7 colors 7import itertools import operator ## only needed if want to play with operators9 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1200 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1201 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) 1202 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1203 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1202 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1205 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1206 = [ operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1209_______ 5 max(5, 3) 5 max(5, 6) 6 max(6, 2) 6 max(6, 1) 6 max(6, 9) 9 max(9, 1) 99 = colors 1import itertools import operator ## only needed if want to play with operators5 import itertools import operator ## only needed if want to play with operators6 import itertools import operator ## only needed if want to play with operators7 colors 7import itertools import operator ## only needed if want to play with operators9 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1200 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1201 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. 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) 1202 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1203 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1202 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1205 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1206 = [ operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1209_______ 5 max(5, 3) 5 max(5, 6) 6 max(6, 2) 6 max(6, 1) 6 max(6, 9) 9 max(9, 1) 99 = colors 1import itertools import operator ## only needed if want to play with operators5 import itertools import operator ## only needed if want to play with operators6 import itertools import operator ## only needed if want to play with operators7 colors 7import itertools import operator ## only needed if want to play with operators9 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1200 operator.mul(1, 2) 2 operator.mul(2, 3) 6 operator.mul(6, 4) 24 operator.mul(24, 5) 1201 |