Chúng ta có thể sử dụng vòng lặp for để lặp qua danh sách, chuỗi hoặc đối tượng phạm vi. Lý do tại sao chúng ta có thể lặp lại các đối tượng này là vì chúng có thể lặp lại
Iterables
- ví dụ. Danh sách, chuỗi, từ điển, kết nối tệp đều có thể lặp lại
- Sự định nghĩa. Một đối tượng với một phương thức
1
6 được liên kết
2
3
4
5
6
7
8 - Áp dụng
1
6 cho một iterable sẽ tạo ra một iterator. Đây thực sự là những gì vòng lặp for đang làm dưới mui xe
2
3
4
5
6
7
8
Trình lặp
- Sự định nghĩa. Một đối tượng có phương thức
1
8 được liên kết tạo ra các giá trị liên tiếp
2
3
4
5
6
7
8
Tóm lại
- một iterable là một đối tượng có thể trả về một iterator,
- trong khi iterator là một đối tượng giữ trạng thái và tạo ra giá trị tiếp theo khi bạn gọi next[] trên đó
Để tạo một iterator từ một iterable, tất cả những gì chúng ta cần làm là sử dụng hàm
16 và truyền vào iterable
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
word = 'Da' # 'word' is a iterable
it = iter[word] # 'it' is an iterator
next[it] #returns 'D', which is the first letter in the string.
next[it] #returns 'a', which is the second letter in the string
next[it] #no letters left. it throws a iteration stop error
print[*it] #unpacks all elements of an iterator
print[*it] #note that this can only be called once
Để lặp qua từ điển, cần có ______6_______0 khi gọi vòng lặp for
Để lặp qua các kết nối tệp
1
2
3
4
file = open['file.txt']
it = iter[file]
print[next[it]] #print the first line of the txt file
print[next[it]] #print the second line of the txt file
Sử dụng liệt kê[]
avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']1 là một hàm nhận bất kỳ đối tượng có thể lặp lại nào, chẳng hạn như danh sách và trả về một đối tượng liệt kê đặc biệt, bao gồm các cặp chứa các phần tử của có thể lặp lại ban đầu, cùng với chỉ mục của chúng trong phạm vi có thể lặp lại. Chúng ta có thể sử dụng hàm
e = enumerate[avengers]
print[type[e]] #class 'enumerate'
e_list = list[e]
print[e_list] #[[0, 'hawkeye'], [1, 'iron man'], [2, 'thor'], [3, 'quicksilver']]
for index, value in enumerate[avengers, start = 10]: # start specifies the index of the first element in the list
print[index, value]
avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']2 để biến đối tượng liệt kê này thành một danh sách các bộ [chỉ mục, phần tử] và in ra để xem nó chứa gì
e = enumerate[avengers]
print[type[e]] #class 'enumerate'
e_list = list[e]
print[e_list] #[[0, 'hawkeye'], [1, 'iron man'], [2, 'thor'], [3, 'quicksilver']]
for index, value in enumerate[avengers, start = 10]: # start specifies the index of the first element in the list
print[index, value]
1
2
3
4
5
6
7
8
avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']
e = enumerate[avengers]
print[type[e]] #class 'enumerate'
e_list = list[e]
print[e_list] #[[0, 'hawkeye'], [1, 'iron man'], [2, 'thor'], [3, 'quicksilver']]
for index, value in enumerate[avengers, start = 10]: # start specifies the index of the first element in the list
print[index, value]
Sử dụng zip[]
avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']3 chấp nhận số lần lặp tùy ý và trả về một bộ lặp gồm các bộ lặp [list1_element1, list2_element1, list3_element1]
e = enumerate[avengers]
print[type[e]] #class 'enumerate'
e_list = list[e]
print[e_list] #[[0, 'hawkeye'], [1, 'iron man'], [2, 'thor'], [3, 'quicksilver']]
for index, value in enumerate[avengers, start = 10]: # start specifies the index of the first element in the list
print[index, value]
1
2
3
4
5
6
7
8
9
10
11
12
13
avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']
names = ['barton', 'stark', 'odinson', 'maximoff']
z = zip[avengers, names]
print[type[z]] #class 'zip'
z_list = list[z]
print[z_list] #[['hawkeye', 'barton'], ['iron man', 'stark'], ['thor', 'odinson'], ['quicksilver', 'maximoff']]
for z1, z2 in zip[avengers, names]:
print[z1, z2]
#or simply use the splat operator
print[*z] #['hawkeye', 'barton'], ['iron man', 'stark'], ['thor', 'odinson'], ['quicksilver', 'maximoff']
avengers, names = zip[*z] #avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver'], names = ['barton', 'stark', 'odinson', 'maximoff']
Sử dụng trình vòng lặp để tải các tệp lớn
Khi tệp quá lớn để giữ trong bộ nhớ, chúng tôi có thể tải dữ liệu theo khối. Chúng ta có thể thực hiện các thao tác mong muốn trên một đoạn dữ liệu, lưu trữ kết quả, loại bỏ đoạn dữ liệu đó và sau đó tải đoạn dữ liệu tiếp theo. Một iterator hữu ích trong trường hợp này
Chúng tôi sử dụng chức năng gấu trúc.
avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']4 và chỉ định đoạn với
e = enumerate[avengers]
print[type[e]] #class 'enumerate'
e_list = list[e]
print[e_list] #[[0, 'hawkeye'], [1, 'iron man'], [2, 'thor'], [3, 'quicksilver']]
for index, value in enumerate[avengers, start = 10]: # start specifies the index of the first element in the list
print[index, value]
avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']5
e = enumerate[avengers]
print[type[e]] #class 'enumerate'
e_list = list[e]
print[e_list] #[[0, 'hawkeye'], [1, 'iron man'], [2, 'thor'], [3, 'quicksilver']]
for index, value in enumerate[avengers, start = 10]: # start specifies the index of the first element in the list
print[index, value]
1
2
3
4
5
6
7
8
9
10
11
12
13
import pandas as pd
result = []
for chunk in pd.read_csv['data.csv', chunksize = 1000]:
result.append[sum[chunk['x']]]# we want to get the sum of column x
total = sum[result]
print[total]
# Another way
import pandas as pd
total = 0
for chunk in pd.read_csv['data.csv', chunksize = 1000]:
total += sum[chunk['x']] # we want to get the sum of column x
print[total]
Áp dụng thủ thuật trong trường hợp loa tweeter
word = 'Da' # 'word' is a iterable0
it = iter[word] # 'it' is an iterator
next[it] #returns 'D', which is the first letter in the string.
next[it] #returns 'a', which is the second letter in the string
next[it] #no letters left. it throws a iteration stop error
print[*it] #unpacks all elements of an iterator
print[*it] #note that this can only be called once
word = 'Da' # 'word' is a iterable1List Comprehensions
it = iter[word] # 'it' is an iterator
next[it] #returns 'D', which is the first letter in the string.
next[it] #returns 'a', which is the second letter in the string
next[it] #no letters left. it throws a iteration stop error
print[*it] #unpacks all elements of an iterator
print[*it] #note that this can only be called once
Khả năng hiểu danh sách có thể thu gọn đối với các vòng lặp để xây dựng danh sách thành một dòng. Nó tạo danh sách từ các danh sách khác, cột DataFrame, v.v. và hiệu quả hơn vòng lặp for vì nó chỉ cần một dòng mã
Thành phần bắt buộc
- Có thể lặp lại
- Biến iterator [đại diện cho các thành viên của iterable]
- biểu thức đầu ra
Khi chúng tôi có một danh sách số và chúng tôi muốn tạo một danh sách các số mới, giống như danh sách cũ ngoại trừ mỗi số được thêm 1 vào nó. Thay vì sử dụng vòng lặp for trên nhiều dòng, chúng ta có thể sử dụng khả năng hiểu danh sách để kết thúc thao tác này trong một dòng như sau
word = 'Da' # 'word' is a iterable2_______5_______3
it = iter[word] # 'it' is an iterator
next[it] #returns 'D', which is the first letter in the string.
next[it] #returns 'a', which is the second letter in the string
next[it] #no letters left. it throws a iteration stop error
print[*it] #unpacks all elements of an iterator
print[*it] #note that this can only be called once
Khả năng hiểu danh sách không bị giới hạn trong danh sách và có thể được sử dụng trên bất kỳ lần lặp nào
word = 'Da' # 'word' is a iterable4_______5_______5
it = iter[word] # 'it' is an iterator
next[it] #returns 'D', which is the first letter in the string.
next[it] #returns 'a', which is the second letter in the string
next[it] #no letters left. it throws a iteration stop error
print[*it] #unpacks all elements of an iterator
print[*it] #note that this can only be called once
Chúng ta cũng có thể thay thế các vòng lặp lồng nhau bằng cách hiểu danh sách
word = 'Da' # 'word' is a iterable6_______5_______7
it = iter[word] # 'it' is an iterator
next[it] #returns 'D', which is the first letter in the string.
next[it] #returns 'a', which is the second letter in the string
next[it] #no letters left. it throws a iteration stop error
print[*it] #unpacks all elements of an iterator
print[*it] #note that this can only be called once
Để tạo một ma trận bằng cách hiểu danh sách
word = 'Da' # 'word' is a iterable8_______5_______9
it = iter[word] # 'it' is an iterator
next[it] #returns 'D', which is the first letter in the string.
next[it] #returns 'a', which is the second letter in the string
next[it] #no letters left. it throws a iteration stop error
print[*it] #unpacks all elements of an iterator
print[*it] #note that this can only be called once
Hiểu nâng cao
Điều kiện trên iterable
10
2
3
4
11
2
3
4
Điều kiện trên biểu thức đầu ra
10
2
3
4
13
2
3
4
Hiểu từ điển để tạo từ điển
14_______7_______5
2
3
4
máy phát điện
Trình tạo rất giống với khả năng hiểu danh sách, ngoại trừ việc nó không được lưu trong bộ nhớ và không tạo danh sách. Nhưng chúng ta vẫn có thể lặp lại trình tạo để tạo các phần tử của danh sách theo yêu cầu. Nó trở nên rất hữu ích khi bạn không muốn lưu trữ toàn bộ danh sách trong bộ nhớ
16
2
3
4
17
2
3
4
Giả sử chúng ta muốn lặp qua một số lượng lớn các số nguyên từ 0 đến 10 ** 1000000
10
2
3
4
19
2
3
4
Ta cũng có thể áp dụng điều kiện cho máy phát điện
10
2
3
4
file = open['file.txt']1
it = iter[file]
print[next[it]] #print the first line of the txt file
print[next[it]] #print the second line of the txt file
Chức năng máy phát điện
Các hàm tạo là các hàm mà khi được gọi sẽ tạo ra các đối tượng tạo. Nó mang lại một chuỗi các giá trị thay vì trả về một giá trị duy nhất. Nó được định nghĩa giống như các hàm khác, ngoại trừ việc nó tạo ra một giá trị với ____6_______6 thay cho ____6_______7 ở cuối
word = 'Da' # 'word' is a iterable6_______8_______3
it = iter[word] # 'it' is an iterator
next[it] #returns 'D', which is the first letter in the string.
next[it] #returns 'a', which is the second letter in the string
next[it] #no letters left. it throws a iteration stop error
print[*it] #unpacks all elements of an iterator
print[*it] #note that this can only be called once
Một vi dụ khac
file = open['file.txt']4_______8_______5
it = iter[file]
print[next[it]] #print the first line of the txt file
print[next[it]] #print the second line of the txt file
avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']8 và
e = enumerate[avengers]
print[type[e]] #class 'enumerate'
e_list = list[e]
print[e_list] #[[0, 'hawkeye'], [1, 'iron man'], [2, 'thor'], [3, 'quicksilver']]
for index, value in enumerate[avengers, start = 10]: # start specifies the index of the first element in the list
print[index, value]
avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']9 thực sự cũng tạo ra các trình tạo đằng sau hậu trường khi chúng được gọinghiên cứu điển hình. Dữ liệu của ngân hàng thế giới_______8_______6_______8_______7
e = enumerate[avengers]
print[type[e]] #class 'enumerate'
e_list = list[e]
print[e_list] #[[0, 'hawkeye'], [1, 'iron man'], [2, 'thor'], [3, 'quicksilver']]
for index, value in enumerate[avengers, start = 10]: # start specifies the index of the first element in the list
print[index, value]
Biến từ điển thành khung dữ liệu
16
2
3
4
file = open['file.txt']9
it = iter[file]
print[next[it]] #print the first line of the txt file
print[next[it]] #print the second line of the txt file
Viết một trình tạo để tải từng dòng dữ liệu
Sử dụng trình tạo để tải từng dòng tệp. Nếu dữ liệu đang truyền trực tuyến, nghĩa là nếu có thêm dữ liệu được thêm vào tập dữ liệu trong khi bạn thực hiện thao tác, thì dữ liệu sẽ đọc và xử lý tệp cho đến khi hết tất cả các dòng
quản lý bối cảnh. Tệp csv
10 nằm trong thư mục hiện tại để bạn sử dụng. Để bắt đầu, bạn cần mở một kết nối tới tệp này bằng cách sử dụng trình quản lý ngữ cảnh. Ví dụ: lệnh
2
3
4
5
6
7
8
9
10
11
12
13
11 liên kết tệp csv
2
3
4
5
6
7
8
9
10
11
12
13
12 là
2
3
4
5
6
7
8
9
10
11
12
13
13 trong trình quản lý bối cảnh. Ở đây, câu lệnh
2
3
4
5
6
7
8
9
10
11
12
13
14 là trình quản lý bối cảnh và mục đích của nó là đảm bảo rằng các tài nguyên được phân bổ hiệu quả khi mở kết nối tới một tệp
2
3
4
5
6
7
8
9
10
11
12
13