mr_incredible
mr_incredible

Reputation: 1115

Moving back at forth while in the for loop

I've got an interesting problem to solve. How do I change direction of iteration (go reverse) whilst in iteration ? Let me explain. I've got list like this:

a = ['one', 'two', 'three', 'four']

and say, if the fourth element contains the letter 'r' go back one step and add to the element before that number 3 until the length of the element is less than or equal 10. Something like this:

for index, element in enumerate(a):
    if 'r' in element and len(a[index-1]) <= 10:
        # go back solution comes here
        a[index-1] += '3'
    else:
        continue

It's quite obvious why I can't just stick with a[index-1] += '3'. Because that won't track the position of the iteration, right. Because the program needs to know how many times it needs to go back and extend the element.

Is there Pythonic solution to this ? I'm aware of range(10, 0, -1) and reverse(<iterator>) but that won't help in this case.

Upvotes: 1

Views: 653

Answers (3)

Wololo
Wololo

Reputation: 1295

I guess Sid's answer solved the issue. I just wanted to point out that the while loop isn't really neccessary. In Python, you may multiply strings: 3*"hi " will return hi hi hi, and the final length of each element is known (10).. Below is a neat/ugly (depending on who you ask) one-liner that yields the same result as Sid's solution.

a = ['one', 'two', 'three', 'four']

char = "3" # char to add to chosen elements in list
length = 10 # the final char-length after adding the chars to list elements
trigger = "r" # char to trigger the adding of new chars.

b = [x if (i==len(a)-1 or trigger not in a[i+1]) else x+(char*(length-len(x))) for i, x in enumerate(a)]

print(b) # ['one', 'two3333333', 'three33333', 'four']

Upvotes: 2

mr_incredible
mr_incredible

Reputation: 1115

for index, element in enumerate(a):
    if 'r' in element and len(a[index-1]) <= 10:
        while len(a[index-1]) <= 10:
            a[index-1] += '3'
    else:
        continue

Perhaps not the most Pythonic way to do it but I do find it quite elegant. Depends who you ask.

Upvotes: 0

Sid
Sid

Reputation: 2189

Maybe this is what you were looking for:

a = ['one', 'two', 'three', 'four']
count = 0
while count < len(a):
    element = a[count]
    #print('in loop')
    if 'r' in element and count > -1 and len(a[count-1]) <= 10:
        # go back solution comes here
        #print('going back')
        a[count-1] += '3'
        count -= 1
        #print('done')
        print(a)
    else:
        #print('not found')
        count += 1
        continue

Output:

['one', 'two3', 'three', 'four']
['one', 'two33', 'three', 'four']
['one', 'two333', 'three', 'four']
['one', 'two3333', 'three', 'four']
['one', 'two33333', 'three', 'four']
['one', 'two333333', 'three', 'four']
['one', 'two3333333', 'three', 'four']
['one', 'two33333333', 'three', 'four']
['one', 'two33333333', 'three3', 'four']
['one', 'two33333333', 'three33', 'four']
['one', 'two33333333', 'three333', 'four']
['one', 'two33333333', 'three3333', 'four']
['one', 'two33333333', 'three33333', 'four']
['one', 'two33333333', 'three333333', 'four']

Upvotes: 1

Related Questions