Bạn có thể bỏ qua các vòng lặp và sử dụng
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
3 và nhập screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
4 từ screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
5from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print l
[0, 3, 6, 9]
Và nếu bạn muốn nó như chuỗi bạn có thể làm
from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print map[str, l]
['0','3', '6', '9']
Mục tiêu công thức
Trong Python, trong khi hoạt động trong danh sách, chúng ta có thể cần lưu trữ từng đầu ra vòng lặp với mỗi lần lặp.
Vì vậy, công thức này là một ví dụ ngắn về cách nối đầu ra cho vòng lặp trong danh sách Python. Bắt đầu nào.
Mục lục
- Mục tiêu công thức
- Trong Python, trong khi hoạt động trong danh sách, chúng ta có thể cần lưu trữ từng đầu ra vòng lặp với mỗi lần lặp.
- Vì vậy, công thức này là một ví dụ ngắn về cách nối đầu ra cho vòng lặp trong danh sách Python. Bắt đầu nào.
- Mục lục
- Bước 1 - Thiết lập dữ liệu
Trong Python, trong khi hoạt động trong danh sách, chúng ta có thể cần lưu trữ từng đầu ra vòng lặp với mỗi lần lặp.
6screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
Vì vậy, công thức này là một ví dụ ngắn về cách nối đầu ra cho vòng lặp trong danh sách Python. Bắt đầu nào.
Vì vậy, công thức này là một ví dụ ngắn về cách nối đầu ra cho vòng lặp trong danh sách Python. Bắt đầu nào.
7screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
Mục lục
Mục lục
8screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
Bước 1 - Thiết lập dữ liệu
Bước 1 - Thiết lập dữ liệu
Bước 2 - Danh sách nối tiếp trong A For Loop
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Đăng nhập vào tài khoản Python Barsels của bạn để lưu cài đặt screencast của bạn.
Vẫn chưa có tài khoản? Đăng ký tại đây.
Hãy biến vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 thành một danh sách hiểu.Một vòng lặp screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 xây dựng một danh sách mới
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
Chúng tôi có một danh sách các chuỗi đại diện cho Python Barsels Tên Screencast:
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
Và chúng tôi có một vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 lặp qua danh sách titles = []
for name in screencasts:
titles.append[name.title[]]
2 này và tạo một danh sách mới [titles = []
for name in screencasts:
titles.append[name.title[]]
3] của chuỗi, thay đổi từng chuỗi một chút trên đường đi:titles = []
for name in screencasts:
titles.append[name.title[]]
Chúng tôi đã tự tiêu đề các chuỗi này, vì vậy chữ cái đầu tiên của mỗi từ được viết hoa.
>>> from pprint import pprint
>>> pprint[titles]
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
Sao chép theo cách của chúng tôi để hiểu
Bất cứ khi nào bạn có một vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 xây dựng một danh sách mới và nó được viết ở định dạng này:new_list = []
for item in some_list:
new_list.append[some_operation_with[item]]
Bạn có thể sao chép-dán vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 thành sự hiểu biết.copy-paste that screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 loop into a comprehension.Hãy sao chép vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 của chúng tôi thành một sự hiểu biết:titles = []
for name in screencasts:
titles.append[name.title[]]
Chúng tôi sẽ bắt đầu bản sao của chúng tôi với dấu ngoặc vuông [
titles = []
for name in screencasts:
titles.append[name.title[]]
7]:Khung vuông này cho thấy rằng chúng tôi đang xây dựng một danh sách.
Sau đó, chúng tôi sẽ sao chép những thứ mà chúng tôi đang thêm [
titles = []
for name in screencasts:
titles.append[name.title[]]
8 trong trường hợp của chúng tôi]:titles = [
name.title[]
]
Logic looping của chúng tôi [dòng
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 không có dấu hai chấm] sau đó:titles = [
name.title[]
for name in screencasts
]
Sự hiểu biết đó tương đương với vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 mà chúng tôi đã bắt đầu với:titles = []
for name in screencasts:
titles.append[name.title[]]
Lưu ý rằng chúng tôi đã viết sự hiểu biết của chúng tôi trên nhiều dòng mã. Chúng tôi có thể đã viết tất cả sự hiểu biết trên một dòng mã, nhưng nó sẽ không thể đọc được.
from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print map[str, l]
['0','3', '6', '9']
1Whitespace là bạn của bạn, đặc biệt là khi nói đến khả năng đọc mã. Cá nhân tôi thích viết mọi thành phần của sự hiểu biết của tôi trên một dòng riêng biệt., especially when it comes to code readability. I personally prefer to write every component of my comprehension on a separate line.
Tạo ra sự hiểu biết lọc các mục
Chiến lược này về các vòng lặp sao chép thành các toàn bộ cũng hoạt động khi chúng ta phải lọc các yếu tố xuống [chỉ bao gồm các mục phù hợp với một điều kiện cụ thể].filter elements down [only including items that match a specific condition].
Vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 này xây dựng một danh sách mới, các tên gọi là 30 ký tự trở lên:from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print map[str, l]
['0','3', '6', '9']
2Danh sách kết quả [
>>> from pprint import pprint
>>> pprint[titles]
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
2] chỉ bao gồm các chuỗi dài:from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print map[str, l]
['0','3', '6', '9']
3Bất cứ khi nào bạn gặp phải một vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 được viết ở định dạng này:from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print map[str, l]
['0','3', '6', '9']
4Bạn có thể sao chép theo cách của bạn vào một sự hiểu biết.
Giống như trước đây, điều đầu tiên chúng tôi sẽ sao chép là dấu ngoặc vuông [
titles = []
for name in screencasts:
titles.append[name.title[]]
7]:Điều đầu tiên chúng tôi đặt sau khung hình vuông mở là thứ mà chúng tôi sắp xếp vào danh sách mới của mình:
from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print map[str, l]
['0','3', '6', '9']
5Logic lặp của chúng tôi [dòng
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9] tiếp theo:from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print map[str, l]
['0','3', '6', '9']
6Và điều cuối cùng mà chúng tôi sao chép dán là điều kiện của chúng tôi [dòng
>>> from pprint import pprint
>>> pprint[titles]
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
6]:from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print map[str, l]
['0','3', '6', '9']
7Sự hiểu biết này được viết trên nhiều dòng mã bởi vì tôi thấy nó dễ đọc hơn nếu chúng tôi viết cùng một logic được cắt tất cả trên một dòng mã:written over multiple lines of code because I find it more readable than if we'd written the same logic squished all on to one line of code:
from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print map[str, l]
['0','3', '6', '9']
8Phá vỡ từng thành phần của sự hiểu biết của chúng tôi trên dòng riêng của nó là dễ đọc hơn một chút:
from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print map[str, l]
['0','3', '6', '9']
7Biến các vòng lặp screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 thành một sự hiểu biết
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
Chiến lược sao chép một vòng
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 thành sự hiểu biết thậm chí còn hoạt động trong các trường hợp phức tạp hơn.Ví dụ, các vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 lồng nhau có thể được biến thành toàn diện:[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
0Bạn cũng có thể sao chép theo cách của bạn thành một sự hiểu biết trong một trường hợp lồng nhau.
Miễn là vòng
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 của bạn chỉ bao gồm các dòng screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9, dòng >>> from pprint import pprint
>>> pprint[titles]
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
6 và một dòng new_list = []
for item in some_list:
new_list.append[some_operation_with[item]]
3 duy nhất, bạn có thể sao chép dán theo cách của bạn vào sự hiểu biết.Điều đầu tiên chúng tôi sao chép là [một lần nữa] dấu ngoặc vuông:
Điều đầu tiên chúng tôi đặt bên trong dấu ngoặc vuông của chúng tôi là thứ chúng tôi nối thêm:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
1Sau đó, logic looping của chúng tôi [dòng
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9] và điều kiện của chúng tôi [dòng >>> from pprint import pprint
>>> pprint[titles]
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
6] nhưng không có dấu chấm ở cuối dòng:[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
2Lưu ý rằng trong khi điều chúng tôi nối tiếp đến trước tiên, tất cả các dòng
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 và >>> from pprint import pprint
>>> pprint[titles]
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
6 vẫn theo cùng thứ tự như trong vòng lặp screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 của chúng tôi.Điều gì sẽ xảy ra nếu không thể sao chép trực tiếp là không thể?
Chiến lược sao chép này chỉ hoạt động nếu vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 của bạn được viết ở định dạng chính xác. Nếu bạn gọi new_list = []
for item in some_list:
new_list.append[some_operation_with[item]]
3 nhiều lần hoặc bạn có logic phức tạp hơn một chút trong vòng lặp screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 của bạn [một cái gì đó không chỉ là >>> from pprint import pprint
>>> pprint[titles]
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
6 hoặc screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9] thì bạn không gặp may.Ví dụ: vòng
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 bên dưới có nhiều cuộc gọi new_list = []
for item in some_list:
new_list.append[some_operation_with[item]]
3 và nó có khối titles = []
for name in screencasts:
titles.append[name.title[]]
6:[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
3Trong trường hợp cụ thể này, chúng tôi có thể thu gọn logic có điều kiện của chúng tôi thành một cuộc gọi
new_list = []
for item in some_list:
new_list.append[some_operation_with[item]]
3 nếu chúng tôi thực hiện hàm trợ giúp:into a single new_list = []
for item in some_list:
new_list.append[some_operation_with[item]]
3 call if we make a helper function:[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
4Hàm trợ giúp này chứa cùng một logic có trong khối
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 của chúng tôi ____ 56 -________ 76.Vì chức năng trợ giúp đó đang quan tâm đến logic ____ 56 -____ ____ 76 và trả về một kết quả duy nhất, chúng tôi có thể viết lại vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 của mình để thêm kết quả đó vào danh sách mới của chúng tôi:[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
5Vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 hiện được viết ở định dạng chính xác để sao chép theo cách của chúng tôi thành một sự hiểu biết:[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
6Lưu ý rằng trong trường hợp cụ thể này, một câu lệnh
>>> from pprint import pprint
>>> pprint[titles]
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
6 có thể được sử dụng nếu bạn thực sự muốn tránh chức năng nhà máy, nhưng tôi không đề xuất nó.Bản tóm tắt
Nếu bạn có một vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 được viết ở định dạng này:[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
7Bạn có thể sao chép theo cách của bạn vào một sự hiểu biết tương đương:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
8Nếu vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 của bạn lọc xuống dựa trên một điều kiện:from operator import add
l = map[add,range[0,4,1],range[0,8,2]]
print map[str, l]
['0','3', '6', '9']
4Bạn cũng có thể sao chép-dán theo cách của bạn vào một sự hiểu biết tương đương:
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
0
Vòng lặp của bạn thậm chí có thể có nhiều câu lệnh
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 hoặc >>> from pprint import pprint
>>> pprint[titles]
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
6:screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
1Và nó chỉ có một cuộc gọi
new_list = []
for item in some_list:
new_list.append[some_operation_with[item]]
3 duy nhất, bạn có thể sao chép theo cách của mình từ vòng lặp screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 vào danh sách hiểu.Bạn sẽ luôn sao chép-dán thứ bạn nối đầu tiên theo sau là dòng
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 và >>> from pprint import pprint
>>> pprint[titles]
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
6 theo cùng một thứ tự chúng xuất hiện trong vòng lặp của bạn:screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
2Không quan trọng bạn có bao nhiêu dòng
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 hoặc bao nhiêu dòng >>> from pprint import pprint
>>> pprint[titles]
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
6, miễn là bạn chỉ có một cuộc gọi new_list = []
for item in some_list:
new_list.append[some_operation_with[item]]
3 duy nhất.Nếu vòng lặp
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 của bạn ở định dạng khác [bạn có logic phức tạp hơn trong đó], bạn sẽ chỉ có thể sao chép nó thành một sự hiểu biết nếu bạn có thể tìm cách tái cấu trúc vòng lặp screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 của mình thành định dạng đơn giản hơn.Mặc dù bạn vẫn chưa quen với sự hiểu biết, tôi khuyên bạn nên sao chép theo cách của bạn từ vòng lặp đến sự hiểu biết để neo sự hiểu biết hiện tại của bạn về
screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 vòng lặp với kiến thức mới về sự hiểu biết của bạn.copy-pasting your way from a loop to a comprehension in order to anchor your existing understanding of screencasts = [
"Data structures contain pointers",
"What is self?",
"What is a class?",
"Slicing",
"How to make a function",
"Methods are just functions attached to classes",
]
9 loops with your new knowledge of
comprehensions.