[Tutor] dealing with lists (2)

marcus.luetolf at bluewin.ch marcus.luetolf at bluewin.ch
Thu Feb 24 14:42:54 EST 2022


Hello Experts, 

I try to solve the following problem:

 

I'd like to create  5 lists, each containing 4 sublists : f1, f2, f3, f4,
f5, a total of 20 sublists.

I have a list (named all_letters) contaning 16 characters a, b, c, ...p.

 

I  would liketo  distribute all 16 characters to the 4 sublists 5 times in 5
iterations respectivly  so that

as a condition a pair of characters, p.e. ['a', 'b', .]  or ['a', 'c'.] or
[.'n', 'p'] can appear only once in all 20 sublists.

 

To find the sublists fullfilling this condition I designed the following
code with the intention to change the indices

defined by the variables n and p til the condition is met :

 

all_letters = list('abcdefghijklmnop')

f1 = []

f2 = []

f3 = []

f4 = []

n = 0

p = 1

for dummy_i in range(5):  

    copy_all_letters = all_letters[:]

    lst = 16*copy_all_letters

    f1.append(lst[n+0])

    f1.append(lst[n+p])

    f1.append(lst[n+2*p])

    f1.append(lst[n+3*p])

    f1.sort()

    f2.append(lst[n+4*p])

    f2.append(lst[n+5*p])

    f2.append(lst[n+6*p])

    f2.append(lst[n+7*p])

    f2.sort()

    f3.append(lst[n+8*p])

    f3.append(lst[n+9*p])

    f3.append(lst[n+10*p])

    f3.append(lst[n+11*p])

    f3.sort()

    f4.append(lst[n+12*p])

    f4.append(lst[n+13*p])

    f4.append(lst[n+14*p])

    f4.append(lst[n+15*p])

    f4.sort()

       

    print('f1: ', f1)

    print('f2: ', f2)

    print('f3: ', f3)

    print('f4: ', f4)

    n += 4

    p += 1

    f1 = []

    f2 = []

    f3 = []

    f4 = []

    f5 = []

 

But I'm unable to define the condition mentioned above and asking for your
help.

 



More information about the Tutor mailing list