mardi 25 juin 2019

Equal splitting values in list and forcing for-loop to run after values

Straight to the problem, there is a value and a list. I want to run a for-loop and print after range of items inside the list.

Here is the code:

A, B = 3, [4,2]

for k, i in enumerate(B):
    if k == 0:
        for num in range(i):
            print('First item from list:', num)
    else:
        for num in range(i):
            print('Second item from list:', num)

Output:

First item from list: 0
First item from list: 1
First item from list: 2
First item from list: 3
Second item from list: 0
Second item from list: 1

What I want to achieve?

I want to run for-loop based on, split to equalsize or split based on length of item1 and item2 inside the list! for example A = 3 and first item inside list is 4 and second is 2, I want to split A-value to A1=2 and A2=1, then force for-loop to run 2 twice when it takes first item, and once when it takes second item, but keep in mind in case we have A= 3 and B=[1,2], my split value should be A1= 1 and A2=2, so it is depending on content of list. Keep in mind in case we have A=4, B=[4,2], splitting would be A1=2 and A2=2, or in case A=7 and B=[5,6], so it is evaluating and make some good splitting like A1=4, A2 =3 etc...

for more clarification in case we have,

A, B = 3, [2,2]

Output could be:

First item from list: 0
First item from list: 1
Second item from list: 0

Aucun commentaire:

Enregistrer un commentaire