Hướng dẫn stop for loop after n iteration python - dừng vòng lặp for sau n lần lặp lại python

2

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi có một vòng lặp đơn giản trong tập lệnh Python:

for filename in filenames:
    outline = getinfo(filename)
    outfile.write(outline)

Điều này cho vòng lặp là một phần của tập lệnh lớn hơn trích xuất dữ liệu từ các trang HTML. Tôi có gần 6GB các trang HTML và muốn thực hiện một số lần chạy thử trước khi tôi thử tất cả chúng.

Làm thế nào tôi có thể làm cho vòng lặp ngắt sau một số lần lặp lại (giả sử 100)?

Hướng dẫn stop for loop after n iteration python - dừng vòng lặp for sau n lần lặp lại python

Karl Knechtel

59.3k10 Huy hiệu vàng86 Huy hiệu bạc130 Huy hiệu đồng10 gold badges86 silver badges130 bronze badges

Đã hỏi ngày 11 tháng 6 năm 2013 lúc 17:15Jun 11, 2013 at 17:15

2

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)

Danh sách lát cắt

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
4 sẽ cắt giảm danh sách các tên tệp chỉ với 100 yếu tố đầu tiên.

Đã trả lời ngày 11 tháng 6 năm 2013 lúc 17:17Jun 11, 2013 at 17:17

Kqrkqrkqr

14.4K3 Huy hiệu vàng38 Huy hiệu bạc71 Huy hiệu đồng3 gold badges38 silver badges71 bronze badges

3

Giữ một quầy cho vòng lặp của bạn. Khi quầy của bạn đạt đến, 100, phá vỡ

counter = 0
for filename in filenames:
    if counter == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
    counter += 1

Đã trả lời ngày 11 tháng 6 năm 2013 lúc 17:16Jun 11, 2013 at 17:16

waldol1waldol1waldol1

1.7972 Huy hiệu vàng17 Huy hiệu bạc21 Huy hiệu đồng2 gold badges17 silver badges21 bronze badges

1

Tôi thích câu trả lời của @KQR, nhưng chỉ là một cách tiếp cận khác để xem xét, thay vì lấy 100 đầu tiên, bạn có thể lấy ngẫu nhiên

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
5 nhiều thay thế: Thay vào đó:

from random import sample
for filename in sample(filenames, 10):
    # pass

Đã trả lời ngày 11 tháng 6 năm 2013 lúc 17:25Jun 11, 2013 at 17:25

Hướng dẫn stop for loop after n iteration python - dừng vòng lặp for sau n lần lặp lại python

Jon Clements ♦ Jon ClementsJon Clements

135K32 Huy hiệu vàng240 Huy hiệu bạc273 Huy hiệu Đồng32 gold badges240 silver badges273 bronze badges

3

Sử dụng chức năng tích hợp Enumerate (), có sẵn trong cả Python 2 và 3.

for idx,filename in enumerate(filenames):
    if idx == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)

Cũng nhìn vào điều này.

Đã trả lời ngày 27 tháng 5 năm 2020 lúc 21:22May 27, 2020 at 21:22

Hướng dẫn stop for loop after n iteration python - dừng vòng lặp for sau n lần lặp lại python

Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu về câu lệnh Python

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
6 và cách sử dụng nó để thoát khỏi vòng lặp sớm.: in this tutorial, you’ll learn about the Python
for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
6 statement and how to use it to exit a loop prematurely.

Giới thiệu về Tuyên bố Break Python

Đôi khi, bạn muốn chấm dứt vòng lặp

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
7 hoặc vòng lặp
for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
8 sớm bất kể kết quả của các thử nghiệm có điều kiện. Trong những trường hợp này, bạn có thể sử dụng câu lệnh
for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
6:

break

Code language: Python (python)

Thông thường, bạn sử dụng câu lệnh

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
6 với câu lệnh
counter = 0
for filename in filenames:
    if counter == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
    counter += 1
1 để chấm dứt một vòng lặp khi một điều kiện là
counter = 0
for filename in filenames:
    if counter == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
    counter += 1
2.

Sử dụng Python Break với For Loop

Sau đây cho thấy cách sử dụng câu lệnh

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
6 bên trong vòng lặp
for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
7:

for index in range(n): # more code here if condition: break

Code language: Python (python)

Trong cú pháp này, nếu

counter = 0
for filename in filenames:
    if counter == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
    counter += 1
5 đánh giá thành
counter = 0
for filename in filenames:
    if counter == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
    counter += 1
2, câu lệnh
for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
6 sẽ chấm dứt vòng lặp ngay lập tức. Nó đã thắng được thực hiện các lần lặp lại còn lại.

Ví dụ này cho thấy cách sử dụng câu lệnh

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
6 bên trong vòng lặp
for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
7:

for index in range(0, 10): print(index) if index == 3: break

Code language: Python (python)

Output:

0 1 2 3

Code language: Python (python)

Làm thế nào nó hoạt động.

  • for idx,filename in enumerate(filenames):
        if idx == 100:
            break
        outline= getinfo(filename)
        outfile.write(outline)
    
    3 tạo ra một vòng lặp không xác định.
  • Khi bạn nhập
    for idx,filename in enumerate(filenames):
        if idx == 100:
            break
        outline= getinfo(filename)
        outfile.write(outline)
    
    2, điều kiện
    for idx,filename in enumerate(filenames):
        if idx == 100:
            break
        outline= getinfo(filename)
        outfile.write(outline)
    
    5 đánh giá đúng thực thi câu lệnh break để chấm dứt vòng lặp.

for idx,filename in enumerate(filenames):
    if idx == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
6 trả về
for idx,filename in enumerate(filenames):
    if idx == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
7 bằng chữ thường để bạn có thể nhập
for idx,filename in enumerate(filenames):
    if idx == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
8,
for idx,filename in enumerate(filenames):
    if idx == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
9 hoặc
for idx,filename in enumerate(filenames):
    if idx == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
2 để thoát khỏi chương trình.

for x in range(5): for y in range(5): # terminate the innermost loop if y > 1: break # show coordinates on the screen print(f"({x},{y})")

Code language: Python (python)

Output:

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
0

Ví dụ này sử dụng hai vòng

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
7 để hiển thị tọa độ từ
from random import sample
for filename in sample(filenames, 10):
    # pass
4 đến
from random import sample
for filename in sample(filenames, 10):
    # pass
5 trên màn hình.

Tuyên bố

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
6 trong vòng lặp lồng nhau chấm dứt vòng lặp trong cùng khi
from random import sample
for filename in sample(filenames, 10):
    # pass
7 lớn hơn một.

Do đó, bạn chỉ thấy các tọa độ có giá trị y bằng không và một.

Sử dụng câu lệnh Python Break với một vòng lặp lại

Sau đây cho thấy cách sử dụng câu lệnh

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
6 bên trong vòng lặp
for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
8:

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
1

Ví dụ sau đây sử dụng câu lệnh

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
6 bên trong vòng lặp
for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
8.

Nó sẽ nhắc bạn vào màu sắc yêu thích của bạn. Chương trình sẽ dừng lại khi bạn nhập

for idx,filename in enumerate(filenames):
    if idx == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
2:

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
2

Output:

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)
3

Làm thế nào nó hoạt động.

  • for idx,filename in enumerate(filenames):
        if idx == 100:
            break
        outline= getinfo(filename)
        outfile.write(outline)
    
    3 tạo ra một vòng lặp không xác định.
  • Khi bạn nhập
    for idx,filename in enumerate(filenames):
        if idx == 100:
            break
        outline= getinfo(filename)
        outfile.write(outline)
    
    2, điều kiện
    for idx,filename in enumerate(filenames):
        if idx == 100:
            break
        outline= getinfo(filename)
        outfile.write(outline)
    
    5 đánh giá đúng thực thi câu lệnh break để chấm dứt vòng lặp.
  • for idx,filename in enumerate(filenames):
        if idx == 100:
            break
        outline= getinfo(filename)
        outfile.write(outline)
    
    6 trả về
    for idx,filename in enumerate(filenames):
        if idx == 100:
            break
        outline= getinfo(filename)
        outfile.write(outline)
    
    7 bằng chữ thường để bạn có thể nhập
    for idx,filename in enumerate(filenames):
        if idx == 100:
            break
        outline= getinfo(filename)
        outfile.write(outline)
    
    8,
    for idx,filename in enumerate(filenames):
        if idx == 100:
            break
        outline= getinfo(filename)
        outfile.write(outline)
    
    9 hoặc
    for idx,filename in enumerate(filenames):
        if idx == 100:
            break
        outline= getinfo(filename)
        outfile.write(outline)
    
    2 để thoát khỏi chương trình.

Bản tóm tắt

  • Sử dụng câu lệnh Python
    for filename in filenames[:100]:
        outline= getinfo(filename)
        outfile.write(outline)
    
    6 để chấm dứt một vòng lặp hoặc vòng lặp trong thời gian sớm.

Bạn có thấy hướng dẫn này hữu ích không?

Làm thế nào để bạn dừng một vòng lặp sau một số vòng lặp nhất định trong Python?

Trong Python, việc phá vỡ từ khóa khiến chương trình thoát khỏi vòng lặp sớm.Break khiến chương trình nhảy ra khỏi vòng lặp ngay cả khi vòng lặp cho vòng không chạy số lần được chỉ định.Break khiến chương trình nhảy ra khỏi các vòng lặp ngay cả khi điều kiện logic xác định vòng lặp vẫn còn đúng.the keyword break causes the program to exit a loop early. break causes the program to jump out of for loops even if the for loop hasn't run the specified number of times. break causes the program to jump out of while loops even if the logical condition that defines the loop is still True .

Làm thế nào tôi sẽ dừng một vòng một thời gian sau n số thời gian?

ngủ (1) ở đầu hoặc cuối thân vòng).).

Làm thế nào để bạn dừng một vòng lặp trong Python?

Python cung cấp hai từ khóa chấm dứt một lần lặp vòng lặp sớm: Tuyên bố Break Python ngay lập tức chấm dứt hoàn toàn một vòng lặp.Thực hiện chương trình tiến hành tuyên bố đầu tiên sau cơ thể vòng lặp.Tuyên bố Python tiếp tục ngay lập tức chấm dứt lặp lại vòng lặp hiện tại.The Python break statement immediately terminates a loop entirely. Program execution proceeds to the first statement following the loop body. The Python continue statement immediately terminates the current loop iteration.

Làm thế nào để bạn dừng vòng lặp lặp?

Dừng một vòng lặp..
Sử dụng tuyên bố phá vỡ ..
Viết tình trạng của vòng lặp theo cách trở nên sai khi bạn muốn dừng lặp lại ..