Hộp công cụ khoa học dữ liệu python datacamp (phần 2 github)

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
    2
    3
    4
    5
    6
    7
    8
    6 được liên kết
  • Áp dụng
    1
    2
    3
    4
    5
    6
    7
    8
    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

Trình lặp

  • Sự định nghĩa. Một đối tượng có phương thức
    1
    2
    3
    4
    5
    6
    7
    8
    8 được liên kết tạo ra các giá trị liên tiếp

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

1
2
3
4
5
6
7
8
6 và truyền vào iterable

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']
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 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
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]
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ì

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']
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]
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]

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']
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]
4 và chỉ định đoạn với
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]
5

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 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
0
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
1List Comprehensions

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

  1. Có thể lặp lại
  2. Biến iterator [đại diện cho các thành viên của iterable]
  3. 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 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
2_______5_______3

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 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
4_______5_______5

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 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
6_______5_______7

Để tạo một ma trận bằng cách hiểu danh sách

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
8_______5_______9

Hiểu nâng cao

Điều kiện trên iterable

1
2
3
4
0
1
2
3
4
1

Điều kiện trên biểu thức đầu ra

1
2
3
4
0
1
2
3
4
3

Hiểu từ điển để tạo từ điển

1
2
3
4
4_______7_______5

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ớ

1
2
3
4
6
1
2
3
4
7

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

1
2
3
4
0
1
2
3
4
9

Ta cũng có thể áp dụng điều kiện cho máy phát điện

1
2
3
4
0
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
1

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 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
6_______8_______3

Một vi dụ khac

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
4_______8_______5

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]
8 và
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]
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ọi

nghiên cứu điển hình. Dữ liệu của ngân hàng thế giới_______8_______6_______8_______7

Biến từ điển thành khung dữ liệu

1
2
3
4
6
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
9

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

1
2
3
4
5
6
7
8
9
10
11
12
13
0 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
1
2
3
4
5
6
7
8
9
10
11
12
13
1 liên kết tệp csv
1
2
3
4
5
6
7
8
9
10
11
12
13
2 là
1
2
3
4
5
6
7
8
9
10
11
12
13
3 trong trình quản lý bối cảnh. Ở đây, câu lệnh
1
2
3
4
5
6
7
8
9
10
11
12
13
4 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

Chủ Đề