Hướng dẫn multiple line for loop in python - vòng lặp nhiều dòng trong python

Câu hỏi ban đầu của bạn chỉ định kết quả với cùng tên của dữ liệu (sequences). Ở đây, tôi gán kết quả cho biến result.

Cho rằng bạn không cung cấp dữ liệu mẫu, tôi chỉ tạo một số (sequences).

Sự hiểu biết danh sách ban đầu của bạn được lồng nhau (lưu ý các dấu ngoặc), với mỗi lớp tạo một danh sách riêng.

[[[x.strip() for x in something] for something in something_else] for something_else in sequences].

Tôi đã tạo lại cấu trúc đó bằng cách tạo một danh sách mới với mỗi for-loop, nối thêm khi nó đi cùng để phù hợp với sự hiểu biết danh sách ban đầu.

# Sample data.
sequences = [['the, quick, brown fox'], ['jumped, over, the lazy dog']]

# Solution.
result = []
for sequence in sequences:
    inner_list = list()
    result.append(inner_list)
    for itemset in sequence:
        inner_list_2 = list()
        inner_list.append(inner_list_2)
        for item in itemset.split(","):
            inner_list_2.append(item.strip())
>>> result
[[['the', 'quick', 'brown fox']], [['jumped', 'over', 'the lazy dog']]]

# Original list comprehension.
>>> [[[item.strip() for item in itemset.split(",")] 
      for itemset in sequence] 
     for sequence in sequences]
[[['the', 'quick', 'brown fox']], [['jumped', 'over', 'the lazy dog']]]

Trong bài viết này, chúng tôi sẽ hiểu khái niệm về các tuyên bố đa dòng trong ngôn ngữ lập trình Python.

Tuyên bố trong Python:

Trong Python, một tuyên bố là một lệnh hợp lý mà một thông dịch viên Python có thể đọc và thực hiện. Nó có thể là một tuyên bố gán hoặc một biểu thức trong Python. & Nbsp;

Tuyên bố đa dòng trong Python:

Trong Python, các câu lệnh thường được viết trong một dòng duy nhất và ký tự cuối cùng của các dòng này là Newline. Để mở rộng câu lệnh sang một hoặc nhiều dòng, chúng ta có thể sử dụng niềng răng {}, dấu ngoặc đơn (), vuông [], bán đại tá, và ký tự tiếp tục chém \ \. Chúng tôi có thể sử dụng bất kỳ trong số này theo yêu cầu của chúng tôi trong mã. Với ký tự tiếp tục dòng, chúng ta có thể phân chia rõ ràng một câu lệnh dài thành nhiều dòng (\). & NBSP;

Code:

Python3

g ____10

geeksforgeeks
1

geeksforgeeks
2
geeksforgeeks
3

geeksforgeeks
4

geeksforgeeks
5
geeksforgeeks
6

Trong mã trên nếu chúng ta không sử dụng các ký tự tiếp tục, mã sẽ đưa ra lỗi theo nghĩa đen của chuỗi chưa kết thúc.

Output:

geeksforgeeks

Tiếp tục dòng được chia thành hai cách khác nhau:

  • Tiếp tục dòng rõ ràng
  • Tiếp tục dòng ẩn

Tiếp tục dòng rõ ràng:

Trong loại câu lệnh đa dòng này, chúng tôi sẽ sử dụng ký tự tiếp tục dòng (\) để chia một câu lệnh thành nhiều dòng.

Example:

Trong ví dụ này, chúng tôi đang khởi tạo văn bản và biểu thức toán học bằng cách sử dụng dấu hiệu ‘\ \, đó là sự tiếp tục dòng rõ ràng để tiếp tục cùng một dòng trong nhiều dòng trong lập trình Python.

Python3

geeksforgeeks
7
geeksforgeeks
0
geeksforgeeks
9

geeksforgeeks
2
Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38
1

Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38
2
Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38
3
Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38
4
Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38
5

Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38
2
Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38
7
Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38
4
Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38
9

Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
0
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
1

geeksforgeeks
5
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
3

Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
4
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
5
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
6
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
7

Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
8
geeksforgeeks
0 sequences0 sequences1
geeksforgeeks
3

Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
4sequences4
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
6
geeksforgeeks
3

Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
4sequences8

geeksforgeeks
5result0

Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
4result2
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
6result4

Output:

Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38

Tiếp tục dòng tiềm ẩn:

Trong loại câu lệnh đa dòng này, tiếp tục dòng ẩn được sử dụng khi bạn phân chia một câu lệnh bằng cách sử dụng dấu ngoặc đơn (), dấu ngoặc [] và niềng răng {}. & Nbsp;

Example:

Trong ví dụ này, chúng tôi đang khởi tạo danh sách và biểu thức toán học bằng dấu ngoặc đơn (), dấu ngoặc [] và dấu hiệu {} dấu hiệu là tiếp tục dòng ẩn để tiếp tục cùng một dòng trong nhiều dòng trong lập trình Python.

Python3

result5

geeksforgeeks
0 result7result8result9

sequences0sequences1sequences2sequences3sequences2sequences5sequences2sequences7

sequences0sequences9

geeksforgeeks
5[[[x.strip() for x in something] for something in something_else] for something_else in sequences]1result5 [[[x.strip() for x in something] for something in something_else] for something_else in sequences]3

Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38
2[[[x.strip() for x in something] for something in something_else] for something_else in sequences]5
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
6[[[x.strip() for x in something] for something in something_else] for something_else in sequences]7result5[[[x.strip() for x in something] for something in something_else] for something_else in sequences]9

Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
8
geeksforgeeks
0 for-loop2sequences0 sequences1

for-loop5sequences4

Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
6

for-loop5sequences8[[[x.strip() for x in something] for something in something_else] for something_else in sequences]9

geeksforgeeks
5result0

Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38
2result2
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38
6result4

Output:

Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38