ian lord perdana
ian lord perdana

Reputation: 5

Convert For Loop to While Loop In Python 3.6

for x in a:
    if x[1] in NERTagger:
        kata = ''
        kt = NERTagger[x[1]]
        for y in a:
            if x[0] is not y[0]:
                kata += y[0] + ' '
            elif x[0] == y[0]:
                kata +=  kt + ' '
        hasil.append(kata)

How to transform the code above into the while-loop? because there is an if and for-loop again in the code

Upvotes: 0

Views: 868

Answers (2)

Tomás Gaete
Tomás Gaete

Reputation: 5

There's no problem in using nested for functions.

while is just a general form of for:

for does 3 things:

  • defines a variable.
  • sets a stopping condition.
  • adds 1 to the varaible.

while does 1 of these: sets a stop condition.

Upvotes: 0

prashant
prashant

Reputation: 3318

i = 0

while i < len(a):

  x = a[i]
  i = i + 1
  if x[1] in NERTagger:
    kata = ''
    kt = NERTagger[x[1]]
    j = 0
    while j < len(a):
      y = a[j]
      if x[0] is not y[0]:
          kata += y[0] + ' '
      elif x[0] == y[0]:
          kata +=  kt + ' '
      j = j+1
  hasil.append(kata)

Upvotes: 2

Related Questions