1

I have specific issue where, im trying to find solution on inner loop(execute 3 time) and continue outer to process rest of the list in for loop:

strings = ['A','A','A','A','A','A','A','A','A','A','B','B','B','B','B','B','B','B','B','B',\
           'A','A','A','A','A','A','A','A','A','A','B','B','B','B','B','B','B','B','B','B']

i=0
for string in strings:
    global i
    if string == 'A':
        while i < 3:
            print(string, i)
            i+=1
            if i==3: continue
    elif string== 'B':
         while i < 3:
            print(string,i)
            i+=1
            if i==3: continue
#         print(string)

Current result: A 0 A 1 A 2

Expected to have continued over list once the inner loop complete and process from next:

A 0
A 1
A 2

B 0
B 1
B 2

A 0
A 1
A 2

B 0
B 1
B 2
3
  • Can you explain a little bit about what strings is supposed to be? There are several alternate ways to get your expected output Commented Jun 30, 2022 at 11:37
  • Replace global i with i = 0; you need to reset it per inner loop. Or just replace manual i management using while with for i in range(3):. Either way, get rid of if i == 3: continue (which accomplishes nothing). Commented Jun 30, 2022 at 11:39
  • Hi ShadownRanger,Can you explain with sample code. :strings = ['A','A','A','A','A','A','A','A','A','A','B','B','B','B','B','B','B','B','B','B',\ 'A','A','A','A','A','A','A','A','A','A','B','B','B','B','B','B','B','B','B','B'] for string in strings: i=0 if string == 'A': while i < 3: print(string, i) i+=1 i=0 elif string== 'B': while i < 3: print(string,i) i+=1 i=0 Commented Jun 30, 2022 at 12:15

1 Answer 1

2

If I understand correctly the logic, you could use itertools.groupby to help you form the groups:

variant #1

from itertools import groupby

MAX = 3

for k,g in groupby(strings):
    for i in range(min(len(list(g)), MAX)):
        print(f'{k} {i}')
    print()

variant #2

from itertools import groupby

MAX = 3

for k,g in groupby(strings):
    for i,_ in enumerate(g):
        if i >= MAX:
            break
        print(f'{k} {i}')
    print()

output:

A 0
A 1
A 2

B 0
B 1
B 2

A 0
A 1
A 2

B 0
B 1
B 2

variant #3: without import

prev = None
count = 0
MAX = 3
for s in strings:
    if s == prev:
        if count < MAX:
            print(f'{s} {count}')
            count += 1
    elif prev:
        count = 0
        print()
    prev = s
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Mozway, is there way to count based on, if string == 'A': count and output 3 following string and exit to continue iteration on other list?
not sure what you mean by "other list", maybe this should be a new question where you provide all the reproducible details?
Result is correct as expected, but as i need to check also if there is A and B only in this list. If there is anything else i need to skip the counter. As A & B is relevant not others in the given list. Sorry this is only details i can share. count only if A and B
groupby return the key (here k), you can check that k in {'A', 'B'} in your loop and continue otherwise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.