Home > Back-end >  Python Array List getting values with double different mod
Python Array List getting values with double different mod

Time:01-02

I need help pulling data from a list with different techniques in python

For example: We have a list with 20 different values.

lst = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','w']

mod = 5
roundMod= 3

DESIRED OUTPUT

Round 1 :
1 - a,
2 - b,
3 - c,
4 - d,
5 - e,

Round 2 :
1 - a,
2 - b,
3 - c,
4 - d,
5 - e,

Round 3 :
1 - a,
2 - b,
3 - c,
4 - d,
5 - e,

Round 1:
6 - f,
7 - g,
8 - h,
9 - i,
10 - j,

Round 2 :
6 - f,
7 - g,
8 - h,
9 - i,
10 - j,

Round 3 :
6 - f,
7 - g,
8 - h,
9 - i,
10 - j,

I have a mod for getting max 5 values for each round and roundmod for maximum round before getting next 5 element

CodePudding user response:

IIUC, you want to slice the List with stepwise starting/ending points. Use an integer division (//) for this:

List = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','w']

mod = 5
roundMod= 3

for i in range(6): # not sure how the number of "lines" is defined
    d = i//roundMod
    print(f'{i=}, {d=},', List[d*mod:(d 1)*mod])

output:

i=0, d=0, ['a', 'b', 'c', 'd', 'e']
i=1, d=0, ['a', 'b', 'c', 'd', 'e']
i=2, d=0, ['a', 'b', 'c', 'd', 'e']
i=3, d=1, ['f', 'g', 'h', 'i', 'j']
i=4, d=1, ['f', 'g', 'h', 'i', 'j']
i=5, d=1, ['f', 'g', 'h', 'i', 'j']

If you also want to track the round, use divmod:

List = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','w']

mod = 5
roundMod= 3

for i in range(6):
    d,r = divmod(i, roundMod)
    print(f'Round {r 1}: ', List[d*mod:(d 1)*mod])

output:

Round 1:  ['a', 'b', 'c', 'd', 'e']
Round 2:  ['a', 'b', 'c', 'd', 'e']
Round 3:  ['a', 'b', 'c', 'd', 'e']
Round 1:  ['f', 'g', 'h', 'i', 'j']
Round 2:  ['f', 'g', 'h', 'i', 'j']
Round 3:  ['f', 'g', 'h', 'i', 'j']

CodePudding user response:

This seems a job for a generator:

def pull(lst, mod = 5, round_mod = 3):
    counter = 0
    while True:
        start = counter // round_mod
        if start * mod >= len(lst):
            break
        yield lst[start * mod:(start   1)*mod]
        counter  = 1
        
puller = pull(l)

print([x for x in puller])

OUTPUT

[['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'j'], ['f', 'g', 'h', 'i', 'j'], ['f', 'g', 'h', 'i', 'j'], ['k', 'l', 'm', 'n', 'o'], ['k', 'l', 'm', 'n', 'o'], ['k', 'l', 'm', 'n', 'o'], ['p', 'r', 's', 't', 'w'], ['p', 'r', 's', 't', 'w'], ['p', 'r', 's', 't', 'w']]

or, to reproduce exactly your desired output:

for n, x in enumerate(puller):
    print(f'Round {n   1}: {", ".join([f"{i   1} - {v}" for i, v in enumerate(x)])}')

OUTPUT

Round 1: 1 - a, 2 - b, 3 - c, 4 - d, 5 - e
Round 2: 1 - a, 2 - b, 3 - c, 4 - d, 5 - e
Round 3: 1 - a, 2 - b, 3 - c, 4 - d, 5 - e
Round 4: 1 - f, 2 - g, 3 - h, 4 - i, 5 - j
Round 5: 1 - f, 2 - g, 3 - h, 4 - i, 5 - j
Round 6: 1 - f, 2 - g, 3 - h, 4 - i, 5 - j
Round 7: 1 - k, 2 - l, 3 - m, 4 - n, 5 - o
Round 8: 1 - k, 2 - l, 3 - m, 4 - n, 5 - o
Round 9: 1 - k, 2 - l, 3 - m, 4 - n, 5 - o
Round 10: 1 - p, 2 - r, 3 - s, 4 - t, 5 - w
Round 11: 1 - p, 2 - r, 3 - s, 4 - t, 5 - w
Round 12: 1 - p, 2 - r, 3 - s, 4 - t, 5 - w
  • Related