Python 3.8 theo cặp

Trong ví dụ trên, có thể thấy rằng thời gian của hàm map xấp xỉ một nửa so với thời gian của vòng lặp for. Điều này cho thấy itertools là công cụ nhanh, tiết kiệm bộ nhớ

Các loại trình vòng lặp khác nhau được cung cấp bởi mô-đun này là.  

  • Trình vòng lặp vô hạn
  • vòng lặp tổ hợp
  • Chấm dứt vòng lặp

Trình vòng lặp vô hạn

Iterator trong Python là bất kỳ loại Python nào có thể được sử dụng với vòng lặp 'for in'. Các danh sách, bộ dữ liệu, từ điển và bộ trong Python đều là các ví dụ về trình vòng lặp sẵn có. Nhưng không nhất thiết đối tượng lặp phải cạn kiệt, đôi khi nó có thể là vô hạn. Các loại trình lặp như vậy được gọi là Trình lặp vô hạn

Python cung cấp ba loại trình lặp vô hạn.  

  • count(bắt đầu, bước). Trình lặp này bắt đầu in từ số “bắt đầu” và in vô hạn. Nếu các bước được đề cập, các số sẽ bị bỏ qua, bước khác là 1 theo mặc định. Xem ví dụ dưới đây để biết cách sử dụng nó với vòng lặp for.
    Ví dụ.

Python3




A B A B A B A B 0

5 10 15 20 25 3014

5 10 15 20 25 3015

A B A B A B A B 2 5 10 15 20 25 3017

5 10 15 20 25 3015

5 10 15 20 25 3019

All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]5 All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]6All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]7 5 10 15 20 25 30235 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 30245 10 15 20 25 3041

5 10 15 20 25 30425 10 15 20 25 3029 All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]6_______08A B A B A B A B 8 5 10 15 20 25 30335 10 15 20 25 3034

5 10 15 20 25 30355 10 15 20 25 3036

5 10 15 20 25 30425 10 15 20 25 30385 10 15 20 25 3034

5 10 15 20 25 30355 10 15 20 25 30815 10 15 20 25 3042A B A B A B A B 8All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]3All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]4

đầu ra

5 10 15 20 25 30
  • chu kỳ (có thể lặp lại). Trình lặp này in tất cả các giá trị theo thứ tự từ vùng chứa đã truyền. Nó khởi động lại quá trình in từ đầu khi tất cả các thành phần được in theo chu kỳ.
    Ví dụ 1.

Python3




A B A B A B A B 0

5 10 15 20 25 3014

5 10 15 20 25 3015

A B A B A B A B 2 5 10 15 20 25 3017

5 10 15 20 25 3015

5 10 15 20 25 3052A B A B A B A B 8 5 10 15 20 25 3054

5 10 15 20 25 3015

5 10 15 20 25 3019

All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]5 All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]6All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]7 5 10 15 20 25 30605 10 15 20 25 30615 10 15 20 25 3041

5 10 15 20 25 30425 10 15 20 25 3029 5 10 15 20 25 30655 10 15 20 25 30665 10 15 20 25 3034

5 10 15 20 25 30355 10 15 20 25 3036

5 10 15 20 25 30425 10 15 20 25 30385 10 15 20 25 3034

5 10 15 20 25 30355 10 15 20 25 3081____142A B A B A B A B 8 All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]3All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]4

5 10 15 20 25 30355 10 15 20 25 30525 10 15 20 25 3081A B A B A B A B 8 5 10 15 20 25 3020

đầu ra

A B A B A B A B

ví dụ 2. Sử dụng chức năng tiếp theo

Python3




A B A B A B A B 0

5 10 15 20 25 3014

5 10 15 20 25 3015

A B A B A B A B 2 5 10 15 20 25 3017

5 10 15 20 25 3015

5 10 15 20 25 3090A B A B A B A B 8 A B A B A B A B 95 10 15 20 25 30935 10 15 20 25 30215 10 15 20 25 30955 10 15 20 25 30215 10 15 20 25 30935 10 15 20 25 3025

5 10 15 20 25 3015

A B A B A B A B 00

A B A B A B A B 01A B A B A B A B 8 A B A B A B A B 03

5 10 15 20 25 3015

5 10 15 20 25 3019

All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]5 All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]6All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]7 All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]85 10 15 20 25 3082A B A B A B A B 115 10 15 20 25 3041

A B A B A B A B 13

5 10 15 20 25 3042A B A B A B A B 15

5 10 15 20 25 30425 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 19A B A B A B A B 20A B A B A B A B 8 All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]3All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]4

Trình lặp kết hợp 
Đầu ra.

5 10 15 20 25 302
  • lặp lại(val, num). Trình lặp này in liên tục giá trị đã truyền với số lần vô hạn. Nếu từ khóa tùy chọn num được đề cập, thì nó sẽ in ra num nhiều lần.
    Ví dụ.

Python3




A B A B A B A B 24

A B A B A B A B 25

5 10 15 20 25 3042

A B A B A B A B 27

A B A B A B A B 2 A B A B A B A B 29

5 10 15 20 25 3042

A B A B A B A B 31

5 10 15 20 25 3081 5 10 15 20 25 3082____034A B A B A B A B 35

5 10 15 20 25 3081 5 10 15 20 25 3082____038A B A B A B A B 39A B A B A B A B 405 10 15 20 25 3021____1323A B A B A B A B 43

đầu ra

5 10 15 20 25 3032

vòng lặp tổ hợp

Các trình tạo đệ quy được sử dụng để đơn giản hóa các cấu trúc tổ hợp như hoán vị, tổ hợp và tích Descartes được gọi là trình lặp tổ hợp.
Trong Python có 4 trình lặp tổ hợp.

  • Sản phẩm(). Công cụ này tính tích cartesian của các lần lặp đầu vào. Để tính tích của một lần lặp với chính nó, chúng tôi sử dụng đối số từ khóa lặp lại tùy chọn để chỉ định số lần lặp lại. Đầu ra của chức năng này là các bộ dữ liệu theo thứ tự được sắp xếp.
    Ví dụ.

Python3




A B A B A B A B 44

A B A B A B A B 45 5 10 15 20 25 3017A B A B A B A B 2 A B A B A B A B 48

5 10 15 20 25 3015

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 52All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]4

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 38A B A B A B A B 575 10 15 20 25 30205 10 15 20 25 30215 10 15 20 25 3022A B A B A B A B 61A B A B A B A B 8 5 10 15 20 25 3022A B A B A B A B 43

5 10 15 20 25 3081A B A B A B A B 66

5 10 15 20 25 3015

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 70All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]4

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 38A B A B A B A B 57A B A B A B A B 765 10 15 20 25 30215 10 15 20 25 30955 10 15 20 25 3021A B A B A B A B 76A B A B A B A B 81A B A B A B A B 82A B A B A B A B 43

5 10 15 20 25 3081A B A B A B A B 66

5 10 15 20 25 3015

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 70All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]4

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 38A B A B A B A B 945 10 15 20 25 3061A B A B A B A B 965 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 303235 10 15 20 25 30200

đầu ra

5 10 15 20 25 3092
  • Hoán vị(). Permutations() như cái tên đã nói lên chính nó được sử dụng để tạo ra tất cả các hoán vị có thể có của một lần lặp. Tất cả các yếu tố được coi là duy nhất dựa trên vị trí của chúng chứ không phải giá trị của chúng. Hàm này nhận một iterable và group_size, nếu giá trị của group_size không được chỉ định hoặc bằng Không thì giá trị của group_size sẽ trở thành độ dài của iterable.
    Ví dụ.

Python3




A B A B A B A B 44

A B A B A B A B 45 5 10 15 20 25 3017A B A B A B A B 2 5 10 15 20 25 30205

5 10 15 20 25 3015

5 10 15 20 25 3081 5 10 15 20 25 3082____1209A B A B A B A B 35

5 10 15 20 25 3081 5 10 15 20 25 3082____0385 10 15 20 25 302145 10 15 20 25 30205 10 15 20 25 3021______076A B A B A B A B 815 10 15 20 25 3022A B A B A B A B 43

5 10 15 20 25 3081A B A B A B A B 66

 

5 10 15 20 25 3081 5 10 15 20 25 3082____1225______035

5 10 15 20 25 3081 5 10 15 20 25 3082____0385 10 15 20 25 302305 10 15 20 25 3061A B A B A B A B 43

5 10 15 20 25 3081A B A B A B A B 66

5 10 15 20 25 3015

5 10 15 20 25 3081 5 10 15 20 25 3082____1238A B A B A B A B 35

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 385 10 15 20 25 30230All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]85 10 15 20 25 30825 10 15 20 25 30245 10 15 20 25 302475 10 15 20 25 3022A B A B A B A B 43

đầu ra

5 10 15 20 25 308
  • Kết hợp(). Trình lặp này in tất cả các kết hợp có thể (không thay thế) của vùng chứa được truyền trong các đối số theo kích thước nhóm đã chỉ định theo thứ tự được sắp xếp.
    Ví dụ.

Python3




5 10 15 20 25 30250

5 10 15 20 25 3015

A B A B A B A B 45 5 10 15 20 25 3017A B A B A B A B 2 5 10 15 20 25 30255

5 10 15 20 25 3015

5 10 15 20 25 3081 5 10 15 20 25 3082____1259____035

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 385 10 15 20 25 302645 10 15 20 25 302655 10 15 20 25 30215 10 15 20 25 3022A B A B A B A B 815 10 15 20 25 3022A B A B A B A B 43

5 10 15 20 25 3081A B A B A B A B 66

5 10 15 20 25 3015

5 10 15 20 25 3081 5 10 15 20 25 3082____1276All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]4

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 385 10 15 20 25 302815 10 15 20 25 30615 10 15 20 25 30215 10 15 20 25 3022A B A B A B A B 43

5 10 15 20 25 3081A B A B A B A B 66

5 10 15 20 25 3015

5 10 15 20 25 3081 5 10 15 20 25 3082____1259____74

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 385 10 15 20 25 30281All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]85 10 15 20 25 30825 10 15 20 25 30225 10 15 20 25 302475 10 15 20 25 3020A B A B A B A B 43

đầu ra

5 10 15 20 25 3096
  • Combinations_with_replacement(). Hàm này trả về một chuỗi con có độ dài n từ các phần tử của khả năng lặp trong đó n là đối số mà hàm nhận để xác định độ dài của các chuỗi con do hàm tạo ra. Các phần tử riêng lẻ có thể tự lặp lại trong hàm tổ hợp_with_replacement.
    Ví dụ.

Python3




5 10 15 20 25 30250

5 10 15 20 25 3015

A B A B A B A B 45 5 10 15 20 25 3017A B A B A B A B 2 5 10 15 20 25 303208

5 10 15 20 25 3015

5 10 15 20 25 3081 5 10 15 20 25 3082____13212All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]4

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 385 10 15 20 25 3032175 10 15 20 25 3032185 10 15 20 25 30215 10 15 20 25 3022A B A B A B A B 43

5 10 15 20 25 3081A B A B A B A B 66

5 10 15 20 25 3015

5 10 15 20 25 3081 5 10 15 20 25 3082____13227All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]4

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 385 10 15 20 25 3032325 10 15 20 25 30205 10 15 20 25 30215 10 15 20 25 3022A B A B A B A B 815 10 15 20 25 3022A B A B A B A B 43

5 10 15 20 25 3081A B A B A B A B 66

5 10 15 20 25 3015

5 10 15 20 25 3081 5 10 15 20 25 3082____13244All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]4

5 10 15 20 25 30815 10 15 20 25 3082A B A B A B A B 385 10 15 20 25 303217All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]85 10 15 20 25 30825 10 15 20 25 30225 10 15 20 25 302475 10 15 20 25 3020A B A B A B A B 43

đầu ra

All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]

Chấm dứt vòng lặp

Trình vòng lặp kết thúc được sử dụng để hoạt động trên các chuỗi đầu vào ngắn và tạo đầu ra dựa trên chức năng của phương thức được sử dụng

Các loại trình vòng lặp kết thúc khác nhau là.  

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

Python3




5 10 15 20 25 303256

5 10 15 20 25 303257

 

 

A B A B A B A B 2 5 10 15 20 25 3017

A B A B A B A B 2 A B A B A B A B 3

 

5 10 15 20 25 303262

5 10 15 20 25 303263A B A B A B A B 8 A B A B A B A B 95 10 15 20 25 30205 10 15 20 25 30215 10 15 20 25 303235 10 15 20 25 30215 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 30665 10 15 20 25 3025

5 10 15 20 25 3015

5 10 15 20 25 303275

5 10 15 20 25 303276

5 10 15 20 25 3081 5 10 15 20 25 3082____13279All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]1A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082____0385 10 15 20 25 303286

5 10 15 20 25 3015

5 10 15 20 25 303275

5 10 15 20 25 303289

5 10 15 20 25 3081 5 10 15 20 25 3082____13292All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]1A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082____0385 10 15 20 25 303299

5 10 15 20 25 3015

5 10 15 20 25 303275

5 10 15 20 25 303276

5 10 15 20 25 3081 5 10 15 20 25 3082____13279All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]1A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082____0385 10 15 20 25 303286

5 10 15 20 25 3015

5 10 15 20 25 303275

5 10 15 20 25 303289

5 10 15 20 25 3081 5 10 15 20 25 3082____13292All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]1A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082____0385 10 15 20 25 303299

đầu ra

5 10 15 20 25 304
  • chuỗi(iter1, iter2. ). Hàm này được sử dụng để in lần lượt tất cả các giá trị trong các mục tiêu có thể lặp lại được đề cập trong các đối số của nó.
    Ví dụ.

Python3




5 10 15 20 25 303256

5 10 15 20 25 309227

5 10 15 20 25 3015

 

A B A B A B A B 2 5 10 15 20 25 3017

 

5 10 15 20 25 303262

5 10 15 20 25 303263A B A B A B A B 8 A B A B A B A B 95 10 15 20 25 30205 10 15 20 25 30215 10 15 20 25 303235 10 15 20 25 30215 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 30665 10 15 20 25 3025

5 10 15 20 25 3015

5 10 15 20 25 309244

5 10 15 20 25 309245A B A B A B A B 8 A B A B A B A B 9_______1205 10 15 20 25 3021A B A B A B A B 115 10 15 20 25 30215 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 3092545 10 15 20 25 3025

5 10 15 20 25 3015

5 10 15 20 25 309257

5 10 15 20 25 309258A B A B A B A B 8 A B A B A B A B 95 10 15 20 25 3092615 10 15 20 25 30215 10 15 20 25 3092635 10 15 20 25 30215 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 303235 10 15 20 25 3025

 

5 10 15 20 25 309269

5 10 15 20 25 3081 5 10 15 20 25 3082____19272All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]1A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082____0385 10 15 20 25 309279

đầu ra

5 10 15 20 25 300
  • chuỗi. from_iterable(). Chức năng này được triển khai tương tự như một chuỗi () nhưng đối số ở đây là danh sách các danh sách hoặc bất kỳ vùng chứa có thể lặp lại nào khác.
    Ví dụ.

Python3




5 10 15 20 25 303256

5 10 15 20 25 309281

 

 

A B A B A B A B 2 5 10 15 20 25 3017

 

 

5 10 15 20 25 303262

5 10 15 20 25 303263A B A B A B A B 8 A B A B A B A B 95 10 15 20 25 30205 10 15 20 25 30215 10 15 20 25 303235 10 15 20 25 30215 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 30665 10 15 20 25 3025

5 10 15 20 25 3015

5 10 15 20 25 309244

5 10 15 20 25 309245A B A B A B A B 8 A B A B A B A B 9_______1205 10 15 20 25 3021A B A B A B A B 115 10 15 20 25 30215 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 3092545 10 15 20 25 3025

5 10 15 20 25 3015

5 10 15 20 25 309257

5 10 15 20 25 309258A B A B A B A B 8 A B A B A B A B 95 10 15 20 25 3092615 10 15 20 25 30215 10 15 20 25 3092635 10 15 20 25 30215 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 303235 10 15 20 25 3025

5 10 15 20 25 3015

5 10 15 20 25 30823

5 10 15 20 25 30824A B A B A B A B 8 5 10 15 20 25 30826

 

5 10 15 20 25 30827

5 10 15 20 25 3081 5 10 15 20 25 3082____19272All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]1A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082____0385 10 15 20 25 30837

đầu ra

5 10 15 20 25 300
  • compress(iter, selector). Trình lặp này chọn một cách có chọn lọc các giá trị để in từ vùng chứa đã truyền theo giá trị danh sách boolean được truyền dưới dạng các đối số khác. Các đối số tương ứng với boolean true được in nếu không thì tất cả đều bị bỏ qua.
    Ví dụ.

Python3




5 10 15 20 25 303256

5 10 15 20 25 30839

 

 

A B A B A B A B 2 5 10 15 20 25 3017

 

 

5 10 15 20 25 30842

5 10 15 20 25 3081 5 10 15 20 25 3082____2845______71A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082A B A B A B A B 385 10 15 20 25 308525 10 15 20 25 30853A B A B A B A B 965 10 15 20 25 30205 10 15 20 25 30215 10 15 20 25 30545 10 15 20 25 30215 10 15 20 25 30545 10 15 20 25 30215 10 15 20 25 30545 10 15 20 25 30215 10 15 20 25 30545 10 15 20 25 30215 10 15 20 25 30205 10 15 20 25 30215 10 15 20 25 30545 10 15 20 25 30215 10 15 20 25 30545 10 15 20 25 30215 10 15 20 25 30205 10 15 20 25 30215 10 15 20 25 30545 10 15 20 25 30215 10 15 20 25 30545 10 15 20 25 30215 10 15 20 25 30545 10 15 20 25 30215 10 15 20 25 30545 10 15 20 25 30200

đầu ra

5 10 15 20 25 302
  • thả trong khi (func, seq). Trình lặp này chỉ bắt đầu in các ký tự sau hàm. trong đối số trả về false lần đầu tiên.
    Ví dụ.

Python3




5 10 15 20 25 303256

5 10 15 20 25 30882

 

 

A B A B A B A B 2 5 10 15 20 25 3017

 

 

5 10 15 20 25 30885

5 10 15 20 25 30886A B A B A B A B 8 A B A B A B A B 9_______1225 10 15 20 25 30215 10 15 20 25 30323____1215 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 30665 10 15 20 25 30215 10 15 20 25 3092615 10 15 20 25 3025

5 10 15 20 25 3015

5 10 15 20 25 309600

5 10 15 20 25 3081 5 10 15 20 25 3082____29603All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]1A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082_______0385 10 15 20 25 3096105 10 15 20 25 309611 5 10 15 20 25 3096125 10 15 20 25 3088 5 10 15 20 25 3022 A B A B A B A B 8A B A B A B A B 8 5 10 15 20 25 30545 10 15 20 25 309618

đầu ra

5 10 15 20 25 303
  • filterfalse(func, seq). Như tên gợi ý, trình vòng lặp này chỉ in các giá trị trả về false cho hàm đã truyền.
    Ví dụ.

Python3




5 10 15 20 25 303256

5 10 15 20 25 309620

5 10 15 20 25 3015

 

A B A B A B A B 2 5 10 15 20 25 3017

5 10 15 20 25 3015

5 10 15 20 25 30885

5 10 15 20 25 30886A B A B A B A B 8 A B A B A B A B 9_______1225 10 15 20 25 30215 10 15 20 25 30323____1215 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 30665 10 15 20 25 30215 10 15 20 25 3092615 10 15 20 25 3025

 

5 10 15 20 25 309639

5 10 15 20 25 3081 5 10 15 20 25 3082____29642All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]1A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082_______0385 10 15 20 25 3096495 10 15 20 25 309611 5 10 15 20 25 3096125 10 15 20 25 3088 5 10 15 20 25 3022 A B A B A B A B 8A B A B A B A B 8 5 10 15 20 25 30545 10 15 20 25 309618

đầu ra

5 10 15 20 25 304
  • islice(có thể lặp lại, bắt đầu, dừng, bước). Trình lặp này in có chọn lọc các giá trị được đề cập trong vùng chứa có thể lặp lại của nó được truyền dưới dạng đối số. Trình lặp này có 4 đối số, bộ chứa có thể lặp lại, vị trí bắt đầu. , vị trí kết thúc và bước.
    Ví dụ.

Python3




5 10 15 20 25 303256

5 10 15 20 25 309659

5 10 15 20 25 3015

5 10 15 20 25 309661

A B A B A B A B 2 5 10 15 20 25 3017

5 10 15 20 25 3015

5 10 15 20 25 30885

5 10 15 20 25 30886A B A B A B A B 8 A B A B A B A B 9_______1225 10 15 20 25 30215 10 15 20 25 30323______1215 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 30665 10 15 20 25 30215 10 15 20 25 3092615 10 15 20 25 30215 10 15 20 25 3092635 10 15 20 25 30215 10 15 20 25 3096815 10 15 20 25 3025

5 10 15 20 25 3042

5 10 15 20 25 309684

5 10 15 20 25 309685

5 10 15 20 25 3081 5 10 15 20 25 3082____29688All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]1A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082_______0385 10 15 20 25 3096955 10 15 20 25 30205 10 15 20 25 3021A B A B A B A B 115 10 15 20 25 30215 10 15 20 25 3022A B A B A B A B 43

đầu ra

5 10 15 20 25 305
  • bản đồ sao(func. , danh sách bộ). Trình lặp này lấy một hàm và danh sách bộ làm đối số và trả về giá trị theo hàm từ mỗi bộ của danh sách.
    Ví dụ.

Python3




5 10 15 20 25 303256

All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]03

5 10 15 20 25 3015

 

A B A B A B A B 2 5 10 15 20 25 3017

5 10 15 20 25 3015

5 10 15 20 25 3015

All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]09

5 10 15 20 25 30886A B A B A B A B 8 All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]125 10 15 20 25 30205 10 15 20 25 30215 10 15 20 25 309263______1215 10 15 20 25 3024All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]185 10 15 20 25 3092615 10 15 20 25 30215 10 15 20 25 303235 10 15 20 25 30215 10 15 20 25 3020All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]185 10 15 20 25 30245 10 15 20 25 30215 10 15 20 25 303235 10 15 20 25 30215 10 15 20 25 309254All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]18All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]315 10 15 20 25 3023_____19___27

5 10 15 20 25 3015

All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]38

All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]39

5 10 15 20 25 3081 5 10 15 20 25 3082____742All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]1A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082____038All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]49All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]505 10 15 20 25 309618

đầu ra

5 10 15 20 25 306
  • taketime(func, iterable). Iterator này ngược lại với dropwhile(), nó in các giá trị cho đến khi hàm trả về false lần đầu tiên.
    Ví dụ.

Python3




5 10 15 20 25 303256

All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]53

5 10 15 20 25 3015

 

A B A B A B A B 2 5 10 15 20 25 3017

5 10 15 20 25 3015

5 10 15 20 25 30885

5 10 15 20 25 30886A B A B A B A B 8 A B A B A B A B 9_______1225 10 15 20 25 30215 10 15 20 25 30323______121A B A B A B A B 115 10 15 20 25 30215 10 15 20 25 30665 10 15 20 25 30215 10 15 20 25 3092615 10 15 20 25 30215 10 15 20 25 3092635 10 15 20 25 30215 10 15 20 25 3096815 10 15 20 25 3025

5 10 15 20 25 3015

All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]77

5 10 15 20 25 3081 5 10 15 20 25 3082____780All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]1A B A B A B A B 85 10 15 20 25 303282

5 10 15 20 25 3081 5 10 15 20 25 3082______038All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]875 10 15 20 25 309611 5 10 15 20 25 3096125 10 15 20 25 3088 5 10 15 20 25 3022 A B A B A B A B 8A B A B A B A B 8 5 10 15 20 25 3054All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]95

đầu ra

5 10 15 20 25 307
  • tee(iterator, count). - Trình lặp này chia vùng chứa thành một số trình vòng lặp được đề cập trong đối số.
    Ví dụ.

Python3




5 10 15 20 25 303256

All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]97

5 10 15 20 25 3015

 

A B A B A B A B 2 5 10 15 20 25 3017

5 10 15 20 25 3015

5 10 15 20 25 30885

5 10 15 20 25 30886A B A B A B A B 8 A B A B A B A B 9_______1225 10 15 20 25 30215 10 15 20 25 30323______121A B A B A B A B 115 10 15 20 25 30215 10 15 20 25 30665 10 15 20 25 30215 10 15 20 25 3092615 10 15 20 25 30215 10 15 20 25 3092635 10 15 20 25 30215 10 15 20 25 3096815 10 15 20 25 3025

5 10 15 20 25 3015

5 10 15 20 25 30421

5 10 15 20 25 30422A B A B A B A B 8 5 10 15 20 25 304245 10 15 20 25 30425

5 10 15 20 25 3015

5 10 15 20 25 30427

5 10 15 20 25 30428

5 10 15 20 25 30429A B A B A B A B 8 5 10 15 20 25 304315 10 15 20 25 3024All the combination of string in sorted order(with replacement) is: [('A', 'A'), ('A', 'B'), ('B', 'B')] All the combination of list in sorted order(with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order(with replacement) is: [(0, ), (1, )]4

5 10 15 20 25 3015

5 10 15 20 25 30435

5 10 15 20 25 3081 5 10 15 20 25 3082______1455 10 15 20 25 304685 10 15 20 25 304695 10 15 20 25 30215 10 15 20 25 304715 10 15 20 25 30472A B A B A B A B 85 10 15 20 25 30474 A B A B A B A B 43

Islice trong Python là gì?

hàm islice() . cú pháp. islice(có thể lặp lại, bắt đầu, dừng, bước)This iterator selectively prints the values mentioned in its iterable container passed as an argument. Syntax: islice(iterable, start, stop, step)

Tại sao Itertool được sử dụng trong Python?

Itertools là một mô-đun trong Python, nó được sử dụng để lặp qua các cấu trúc dữ liệu có thể được chuyển qua bằng vòng lặp for . Cấu trúc dữ liệu như vậy còn được gọi là iterables. Mô-đun này hoạt động như một công cụ nhanh, tiết kiệm bộ nhớ, được sử dụng riêng lẻ hoặc kết hợp để tạo thành đại số lặp.

Itertools có phải là thư viện Python chuẩn không?

Itertools là một mô-đun Python nằm trong một phần của thư viện chuẩn Python 3 .

Chuỗi trong Python là gì?

hàm chain() . Nó nhóm tất cả các lần lặp lại với nhau và tạo ra một lần lặp duy nhất làm đầu ra. a function that takes a series of iterables and returns one iterable. It groups all the iterables together and produces a single iterable as output.

Chủ đề