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]?
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
Jon Clements ♦ Jon Clements♦Jon 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
Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu về câu lệnh Python
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]
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:Code language: Python [python]
break
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:Code language: Python [python]
for index in range[n]: # more code here if condition: break
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:Code language: Python [python]
for index in range[0, 10]: print[index] if index == 3: break
Output:
Code language: Python [python]
0 1 2 3
Làm thế nào nó hoạt động.
3 tạo ra một vòng lặp không xác định.for idx,filename in enumerate[filenames]: if idx == 100: break outline= getinfo[filename] outfile.write[outline]
- Khi bạn nhập
2, điều kiệnfor 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]
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.Code language: Python [python]
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}]"]
Output:
for filename in filenames[:100]:
outline= getinfo[filename]
outfile.write[outline]
0Ví 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]
1Ví 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]
2Output:
for filename in filenames[:100]:
outline= getinfo[filename]
outfile.write[outline]
3Làm thế nào nó hoạt động.
3 tạo ra một vòng lặp không xác định.for idx,filename in enumerate[filenames]: if idx == 100: break outline= getinfo[filename] outfile.write[outline]
- Khi bạn nhập
2, điều kiệnfor 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ậpfor 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ặcfor 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 idx,filename in enumerate[filenames]: if idx == 100: break outline= getinfo[filename] outfile.write[outline]
Bản tóm tắt
- Sử dụng câu lệnh Python
6 để chấm dứt một vòng lặp hoặc vòng lặp trong thời gian sớm.for filename in filenames[:100]: outline= getinfo[filename] outfile.write[outline]
Bạn có thấy hướng dẫn này hữu ích không?