Python’da iç içe looplardan çıkmak

Aralık 19th, 2011 § Yorum yok

Python’da iç içe looplardan çıkmak

      # Break out of a nested for loop when 'emma' is reached
      # Python does not have a goto statement to do this!

      print
      # or raise an exception ...
      try:
        for list2 in list1:
          for item in list2:
            print item
           if item == 'emma':
             # raise an exception
             raise StopIteration()
      except StopIteration:
        pass
       

      # or use a function and force a return ...


      def break_nestedloop(list1):
        for list2 in list1:
          for item in list2:
            print item
              if item == 'emma':
              # force a return
              return
       
       dummy = break_nestedloop(list1) # returns None

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Ne yapıyorum ben!?

Python’da iç içe looplardan çıkmak başlıklı yazıyı okuyorsun.

Üst Veri