Hướng dẫn islice in python - islice trong trăn

Trong Python, itertools là mô -đun sẵn có cho phép chúng tôi xử lý các trình lặp một cách hiệu quả. Chúng làm cho việc lặp lại thông qua các vòng lặp như danh sách và chuỗi rất dễ dàng. Một hàm itertools như vậy là islice ().islice().

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

hàm islice ()

Trình lặp này có chọn lọc các giá trị được đề cập trong container có thể đi được truyền như một đối số.

Syntax:

islice(iterable, start, stop, step)

Ví dụ 1:

0
1
2
3
4
[4, 7, 10]
2
0
1
2
3
4
[4, 7, 10]
3
0
1
2
3
4
[4, 7, 10]
4
0
1
2
3
4
[4, 7, 10]
5

0
1
2
3
4
[4, 7, 10]
6
0
1
2
3
4
[4, 7, 10]
7
0
1
2
3
4
[4, 7, 10]
8
0
1
2
3
4
[4, 7, 10]
9
1
2
3
4
0
1
2
3
4
1
1
2
3
4
2223
1
2
3
4
4
1
2
3
4
5

1
2
3
4
6
1
2
3
4
7
1
2
3
4
8

1
2
3
4
9
1
3
0
1
3
1
1
3
2
1
3
3
1
3
4
1
3
3
1
2
3
4
4
1
3
3
1
3
8
1
3
3
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
0
1
3
3
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
2
1
3
3
1
2
3
4
222

1
2
3
4
7
1
2
3
4
1
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
8
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
9
The list values till 1st false value are : [2, 4, 6]
The iterators are : 
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
0
1
3
3
The list values till 1st false value are : [2, 4, 6]
The iterators are : 
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
2
1
3
3
1
3
2
The list values till 1st false value are : [2, 4, 6]
The iterators are : 
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
5

Output:

0
1
2
3
4
[4, 7, 10]

Ví dụ 2:

0
1
2
3
4
[4, 7, 10]
2
0
1
2
3
4
[4, 7, 10]
3
0
1
2
3
4
[4, 7, 10]
4
0
1
2
3
4
[4, 7, 10]
5

0
1
2
3
4
[4, 7, 10]
6
0
1
2
3
4
[4, 7, 10]
7
0
1
2
3
4
[4, 7, 10]
8
0
1
2
3
4
[4, 7, 10]
9
1
2
3
4
0
1
2
3
4
1
1
2
3
4
2223
1
2
3
4
4
1
2
3
4
5

1
2
3
4
6
1
2
3
4
7
1
2
3
4
8

Output:

1
2
3
4

1
2
3
4
9
1
3
0
1
3
1
1
3
2
1
3
3
1
3
4
1
3
3
1
2
3
4
4
1
3
3
1
3
8
1
3
3
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
0
1
3
3
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
2
1
3
3
1
2
3
4
222

Ví dụ 2:

0
1
2
3
4
[4, 7, 10]
2
0
1
2
3
4
[4, 7, 10]
3
0
1
2
3
4
[4, 7, 10]
4
0
1
2
3
4
[4, 7, 10]
5

0
1
2
3
4
[4, 7, 10]
6
0
1
2
3
4
[4, 7, 10]
7
0
1
2
3
4
[4, 7, 10]
8
0
1
2
3
4
[4, 7, 10]
9
1
2
3
4
0
1
2
3
4
1
1
2
3
4
2223
1
2
3
4
4
1
2
3
4
5

1
2
3
4
6
1
2
3
4
7
1
2
3
4
8

Output:

1
3

1
2
3
4
9
1
3
0
1
3
1
1
3
2
1
3
3
1
3
4
1
3
3
1
2
3
4
4
1
3
3
1
3
8
1
3
3
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
0
1
3
3
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
2
1
3
3
1
2
3
4
222


2 Kích thước của mỗi nhóm hoán vị được quyết định bởi đối số nhóm_size.

1
2
3
4
7
1
2
3
4
1
1
2
3
4
57
1
2
3
4
17
:- This iterator selectively prints the values mentioned in its iterable container passed as argument. This iterator takes 4 arguments, iterable container, starting pos., ending position and step.

1
2
3
4
7
1
2
3
4
1
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
8
1
2
3
4
62
1
2
3
4
63
1
2
3
4
28
1
2
3
4
65__
:- This iterator takes a function and tuple list as argument and returns the value according to the function from each tuple of list.

0
1
2
3
4
[4, 7, 10]
4
iterator.count(5,2) prints -- 5,7,9,11...infinitely
9

1
2
3
4
7
1
2
3
4
1
1
2
3
4
40
1
2
3
4
17

Is

Máy lặp kết hợp

1. Sản phẩm (ITER1, ITER2):- Iterator này in sản phẩm Cartesian của hai container có thể lặp lại được truyền dưới dạng đối số.

2 Kích thước của mỗi nhóm hoán vị được quyết định bởi đối số nhóm_size.

1
2
3
4
7
1
2
3
4
1
1
2
3
4
57
1
2
3
4
17

Output:

The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]

1
2
3
4
7
1
2
3
4
1
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
8
1
2
3
4
62
1
2
3
4
63
1
2
3
4
28
1
2
3
4
65__
:- This iterator is opposite of dropwhile(), it prints the values till the function returns false for 1st time.

1
2
3
4
7
1
2
3
4
1
1
2
3
4
69
1
2
3
4
17
:- This iterator splits the container into a number of iterators mentioned in the argument.

0
1
2
3
4
[4, 7, 10]
4
iterator.count(5,2) prints -- 5,7,9,11...infinitely
9

1
2
3
4
9
1
3
0
1
3
1
1
3
2
1
3
3
1
3
4
1
3
3
The list values till 1st false value are : [2, 4, 6]
The iterators are : 
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
223333338333333334043333333333333333333

0
1
2
3
4
[4, 7, 10]
91
1
3
0
0
1
2
3
4
[4, 7, 10]
93
0
1
2
3
4
[4, 7, 10]
94

1
2
3
4
7
1
2
3
4
1
0
1
2
3
4
[4, 7, 10]
97
0
1
2
3
4
[4, 7, 10]
47
1
3
0
0
1
2
3
4
[4, 7, 10]
49

1
2
3
4
7
1
2
3
4
1
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
8
1
2
3
4
04
1
2
3
4
05
1
2
3
4
06
1
2
3
4
07____32____
1
3
03030

1
2
3
4
13
1
3
0
1
2
3
4
15
1
2
3
4
16
1
2
3
4
17

1
2
3
4
7
1
2
3
4
1
1
2
3
4
20
1
2
3
4
17

0
1
2
3
4
[4, 7, 10]
6
0
1
2
3
4
[4, 7, 10]
7
0
1
2
3
4
[4, 7, 10]
8
1
2
3
4
0

1
2
3
4
6
1
2
3
4
7
1
2
3
4
1
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
8
1
2
3
4
35

Output:

The list values till 1st false value are : [2, 4, 6]
The iterators are : 
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]

5. Zip_Longest (ITBER1, ITHER2, FILLVAL.):- Trình lặp này in các giá trị của các vòng lặp thay thế theo trình tự. Nếu một trong các vòng lặp được in đầy đủ, các giá trị còn lại được lấp đầy bởi các giá trị được gán cho fillvalue. :- This iterator prints the values of iterables alternatively in sequence. If one of the iterables is printed fully, remaining values are filled by the values assigned to fillvalue.

0
1
2
3
4
[4, 7, 10]
4
iterator.count(5,2) prints -- 5,7,9,11...infinitely
9

1
2
3
4
7
1
2
3
4
1
1
2
3
4
40
1
2
3
4
17

Is

Output:

The combined values of iterables is  : 
('G', 'e') ('e', 'k') ('s', 'f') ('o', 'r') ('G', 'e') ('e', 'k') ('s', '_')

Máy lặp kết hợp

1. Sản phẩm (ITER1, ITER2):- Iterator này in sản phẩm Cartesian của hai container có thể lặp lại được truyền dưới dạng đối số. :- This iterator prints the cartesian product of the two iterable containers passed as arguments.

2 Kích thước của mỗi nhóm hoán vị được quyết định bởi đối số nhóm_size. :- This iterator prints all possible permutation of all elements of iterable. The size of each permuted group is decided by group_size argument.

0
1
2
3
4
[4, 7, 10]
4
iterator.count(5,2) prints -- 5,7,9,11...infinitely
9

1
2
3
4
7
1
2
3
4
1
1
2
3
4
57
1
2
3
4
17

1
2
3
4
7
1
2
3
4
1
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
8
1
2
3
4
62
1
2
3
4
63
1
2
3
4
28
1
2
3
4
65__

1
2
3
4
7
1
2
3
4
1
1
2
3
4
69
1
2
3
4
17

Is

Output:

The cartesian product of the containers is : 
[('A', '1'), ('A', '2'), ('B', '1'), ('B', '2')]
All the permutations of the given container is : 
[('G', 'f'), ('G', 'G'), ('f', 'G'), ('f', 'G'), ('G', 'G'), ('G', 'f')]

3. Kết hợp (ITEBLE, GROUP_SIZE):- Trình lặp này in tất cả các kết hợp có thể có (không có thay thế) của container được truyền trong các đối số theo kích thước nhóm được chỉ định theo thứ tự được sắp xếp.:- This iterator prints all the possible combinations(without replacement) of the container passed in arguments in the specified group size in sorted order.

4. Kết hợp_with_replocation (ITBERS, GROOT_SIZE):- Trình lặp này in tất cả các kết hợp có thể (với sự thay thế) của container được truyền trong các đối số theo kích thước nhóm được chỉ định theo thứ tự được sắp xếp.:- This iterator prints all the possible combinations(with replacement) of the container passed in arguments in the specified group size in sorted order.

0
1
2
3
4
[4, 7, 10]
4
iterator.count(5,2) prints -- 5,7,9,11...infinitely
9

1
2
3
4
7
1
2
3
4
1
1
2
3
4
83
1
2
3
4
17

Is

1
2
3
4
7
1
2
3
4
1
1
2
3
4
95
1
2
3
4
17

Is

Output:

All the combination of container in sorted order(without replacement) is : 
[('1', '2'), ('1', '3'), ('1', '4'), ('2', '3'), ('2', '4'), ('3', '4')]
All the combination of container in sorted order(with replacement) is : 
[('G', 'G'), ('G', 'f'), ('G', 'G'), ('f', 'f'), ('f', 'G'), ('G', 'G')]

Vòng lặp vô hạn

1. Đếm (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ố được bỏ qua, bước khác là 1 theo mặc định. :- This iterator starts printing from the “start” number and prints infinitely. If steps are mentioned, the numbers are skipped else step is 1 by default.

Thí dụ :

iterator.count(5,2) prints -- 5,7,9,11...infinitely

2. 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ừ thùng chứa được truyền. Nó khởi động lại in từ bắt đầu lại khi tất cả các yếu tố được in theo cách theo chu kỳ. :- This iterator prints all values in order from the passed container. It restarts printing from beginning again when all elements are printed in a cyclic manner.

Thí dụ :

0
1
2
3
4
[4, 7, 10]
0

2. 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ừ thùng chứa được truyền. Nó khởi động lại in từ bắt đầu lại khi tất cả các yếu tố được in theo cách theo chu kỳ. :- This iterator repeatedly prints the passed value infinite number of times. If num. is mentioned, them till that number.

0
1
2
3
4
[4, 7, 10]
4
iterator.count(5,2) prints -- 5,7,9,11...infinitely
9

3. Lặp lại (val, num):- Trình lặp này liên tục in số lượng giá trị được truyền vô hạn số lần. Nếu num. được đề cập, chúng cho đến số đó.

1
2
3
4
7
1
2
3
4
1
1
3
09
1
2
3
4
17

Output:

0
1
2
3
4
[4, 7, 10]
1

1
2
3
4
7
1
2
3
4
1
The sliced list values are : [4, 7, 10]
The values acc. to function are : [1, 1, 4, 1]
8
1
3
14
1
3
15
1
2
3
4
28
1
3
4__Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

Bài viết này được đóng góp bởi Manjeet Singh. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng cách sử dụng PROPTENT.GeekSforGeeks.org hoặc gửi bài viết của bạn đến. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.