Hướng dẫn how to break double for loop in python - cách ngắt vòng lặp for trong python

Có lẽ không phải những gì bạn đang hy vọng, nhưng thông thường bạn sẽ muốn có một for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6 sau khi đặt for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 7 thành for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 8

for word1 in buf1: find = False for word2 in buf2: ... if res == res1: print "BINGO " + word1 + ":" + word2 find = True break # <-- break here too if find: break

Một cách khác là sử dụng biểu thức máy phát để ép for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 9 thành một vòng lặp duy nhất

for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break

Bạn cũng có thể xem xét sử dụng from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 0

from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break

Bài viết này mô tả cách thoát ra khỏi các vòng lặp lồng nhau trong Python.

  • Cách viết các vòng lồng nhau trong Python
  • Thoát khỏi các vòng lặp lồng nhau với from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 1 và from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 2
  • Thoát ra khỏi các vòng lồng nhau với một biến cờ
  • Cách sử dụng trên from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 1 và from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 2 có thể khó hiểu đối với những người không quen thuộc với Python.
  • So sánh tốc độ

Kết quả của việc đo thời gian thực hiện của mỗi cách với lệnh ma thuật for i in l1: for j in l2: print(i, j) if i == 2 and j == 20: print('BREAK') break else: continue break # 1 10 # 1 20 # 1 30 # 2 10 # 2 20 # BREAK 8 của Jupyter Notebook được hiển thị. Lưu ý rằng nó không thể được đo nếu được thực thi dưới dạng mã Python.

  • cho vòng lặp trong Python (với phạm vi, liệt kê, zip, v.v.)

Cách viết các vòng lồng nhau trong Python

Tránh các vòng lặp lồng với from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 3

l1 = [1, 2, 3] l2 = [10, 20, 30] for i in l1: for j in l2: print(i, j) # 1 10 # 1 20 # 1 30 # 2 10 # 2 20 # 2 30 # 3 10 # 3 20 # 3 30

Khi for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6 được thực thi trong vòng lặp bên trong, nó chỉ thoát ra khỏi vòng bên trong và vòng lặp bên ngoài tiếp tục.

for i in l1: for j in l2: print(i, j) if i == 2 and j == 20 : print('BREAK') break # 1 10 # 1 20 # 1 30 # 2 10 # 2 20 # BREAK # 3 10 # 3 20 # 3 30

Thoát khỏi các vòng lặp lồng nhau với from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 1 và from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 2

Trong vòng lặp for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 9 của Python, bạn có thể sử dụng from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 1 và from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 2 ngoài for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6.

  • cho vòng lặp trong Python (với phạm vi, liệt kê, zip, v.v.)

Bạn có thể phá vỡ tất cả các vòng lặp với from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 1 và from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 2.

for i in l1: for j in l2: print(i, j) if i == 2 and j == 20: print('BREAK') break else: continue break # 1 10 # 1 20 # 1 30 # 2 10 # 2 20 # BREAK

Mã với lời giải thích như sau.

for i in l1: print('Start outer loop') for j in l2: print('--', i, j) if i == 2 and j == 20: print('-- BREAK inner loop') break else: print('-- Finish inner loop without BREAK') continue print('BREAK outer loop') break # Start outer loop # -- 1 10 # -- 1 20 # -- 1 30 # -- Finish inner loop without BREAK # Start outer loop # -- 2 10 # -- 2 20 # -- BREAK inner loop # BREAK outer loop

Khi vòng lặp bên trong kết thúc bình thường mà không có for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6, from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 2 trong mệnh đề from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 1 được thực thi. from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 2 này dành cho vòng ngoài và bỏ qua for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6 ở vòng ngoài và tiếp tục đến chu kỳ tiếp theo.

Khi vòng lặp bên trong kết thúc với for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6, from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 2 trong mệnh đề from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 1 không được thực thi. Trong trường hợp này, for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6 trong vòng lặp bên ngoài được thực thi.

Kết quả là, bất cứ khi nào vòng lặp bên trong kết thúc với for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6, for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6 ở vòng ngoài cũng được thực hiện.

Ý tưởng là như nhau ngay cả khi số lượng vòng lặp tăng lên. Một ví dụ về một vòng ba là như sau.

l1 = [1, 2, 3] l2 = [10, 20, 30] l3 = [100, 200, 300] for i in l1: for j in l2: for k in l3: print(i, j, k) if i == 2 and j == 20 and k == 200: print('BREAK') break else: continue break else: continue break # 1 10 100 # 1 10 200 # 1 10 300 # 1 20 100 # 1 20 200 # 1 20 300 # 1 30 100 # 1 30 200 # 1 30 300 # 2 10 100 # 2 10 200 # 2 10 300 # 2 20 100 # 2 20 200 # BREAK

Thoát ra khỏi các vòng lồng nhau với một biến cờ

Cách sử dụng trên from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 1 và from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 2 có thể khó hiểu đối với những người không quen thuộc với Python.

Thêm một biến cờ có thể làm cho mã dễ hiểu hơn cho nhiều người.

Trong điều kiện vòng lặp bên trong kết thúc bằng for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6, đặt cờ thành for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 8 và ở vòng ngoài, đặt for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6 theo cờ.

Double-loop:

l1 = [1, 2, 3] l2 = [10, 20, 30] flag = False for i in l1: for j in l2: print(i, j) if i == 2 and j == 20: flag = True print('BREAK') break if flag: break # 1 10 # 1 20 # 1 30 # 2 10 # 2 20 # BREAK

Triple-loop:

l1 = [1, 2, 3] l2 = [10, 20, 30] l3 = [100, 200, 300] flag = False for i in l1: for j in l2: for k in l3: print(i, j, k) if i == 2 and j == 20 and k == 200: flag = True print('BREAK') break if flag: break if flag: break # 1 10 100 # 1 10 200 # 1 10 300 # 1 20 100 # 1 20 200 # 1 20 300 # 1 30 100 # 1 30 200 # 1 30 300 # 2 10 100 # 2 10 200 # 2 10 300 # 2 20 100 # 2 20 200 # BREAK

Bạn có thể tránh các vòng lồng nhau với from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 3.

  • Sản phẩm của Danh sách Cartesian trong Python (itertools.product)

Bạn có thể sử dụng from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 3 để có được tất cả các kết hợp của nhiều danh sách trong một vòng lặp và nhận được kết quả tương tự như các vòng lặp lồng nhau.

for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 0

Vì nó là một vòng lặp duy nhất, bạn chỉ có thể for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 6 trong các điều kiện mong muốn.

for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 1

Thêm đối số của from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 3, bạn có thể thực hiện quy trình tương ứng với nhiều vòng lặp hơn.

for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 2

Ghi chú

Trong from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 3, quy trình cho phần tử luôn được thực hiện cho tất cả các kết hợp.

Trong ví dụ sau, phép nhân được thực hiện 9 lần cho cả for i in l1: for j in l2: print(i, j) if i == 2 and j == 20: print('BREAK') break else: continue break # 1 10 # 1 20 # 1 30 # 2 10 # 2 20 # BREAK 5 và for i in l1: for j in l2: print(i, j) if i == 2 and j == 20: print('BREAK') break else: continue break # 1 10 # 1 20 # 1 30 # 2 10 # 2 20 # BREAK 6.

for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 3

Trong trường hợp các vòng lặp lồng nhau, quá trình cho vòng lặp bên ngoài được thực hiện bởi số lượng các phần tử bên ngoài.

Trong ví dụ sau, phép nhân cho biến for i in l1: for j in l2: print(i, j) if i == 2 and j == 20: print('BREAK') break else: continue break # 1 10 # 1 20 # 1 30 # 2 10 # 2 20 # BREAK 5 chỉ là 3 lần.

for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 4

So sánh tốc độ

Kết quả của việc đo thời gian thực hiện của mỗi cách với lệnh ma thuật for i in l1: for j in l2: print(i, j) if i == 2 and j == 20: print('BREAK') break else: continue break # 1 10 # 1 20 # 1 30 # 2 10 # 2 20 # BREAK 8 của Jupyter Notebook được hiển thị. Lưu ý rằng nó không thể được đo nếu được thực thi dưới dạng mã Python.

  • Đo thời gian thực hiện với thời gian trong Python

Xin lưu ý rằng kết quả sẽ khác nhau tùy thuộc vào số lượng phần tử và số lượng vòng lặp for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 9 được lồng.

Lấy một vòng ba với 100 yếu tố làm ví dụ.

for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 5

Sử dụng from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 1, from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 2 và thêm các biến cờ gần tương đương và from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 3 chậm.

Tuy nhiên, trong một số trường hợp, from itertools import product for word1, word2 in product(buf1, buf2): ... if res == res1: print "BINGO " + word1 + ":" + word2 break 3 phù hợp hơn vì nó cải thiện khả năng đọc của mã ngay cả khi nó chậm. Bạn nên sử dụng nó tùy thuộc vào tình huống.

Break trong đôi cho vòng lặp là gì?

Sử dụng phá vỡ trong một vòng lặp lồng nhau trong một vòng lặp lồng nhau, một câu lệnh ngắt chỉ dừng vòng lặp nó được đặt vào. Do đó, nếu một lần ngắt được đặt trong vòng lặp bên trong, vòng lặp bên ngoài vẫn tiếp tục.Tuy nhiên, nếu ngắt được đặt ở vòng ngoài, tất cả các điểm dừng lại.only stops the loop it is placed in. Therefore, if a break is placed in the inner loop, the outer loop still continues. However, if the break is placed in the outer loop, all of the looping stops.

Bạn có thể có 2 vòng cho Python không?

Lồng nhau cho các vòng lặp có thể được lồng trong Python, vì chúng có thể với các ngôn ngữ lập trình khác.Chương trình lần đầu tiên gặp vòng lặp bên ngoài, thực hiện lần lặp đầu tiên.Lần lặp đầu tiên này kích hoạt vòng lặp bên trong, lồng nhau, sau đó chạy để hoàn thành.Loops can be nested in Python, as they can with other programming languages. The program first encounters the outer loop, executing its first iteration. This first iteration triggers the inner, nested loop, which then runs to completion.

Chủ đề