[Tutor] dealing with lists (2)
marcus.luetolf at bluewin.ch
marcus.luetolf at bluewin.ch
Fri Feb 25 09:45:53 EST 2022
Sorry, I sent my following comment to the wrong address :
Yes, you are right.
But but my final goal is to create 20 sublistst with item-pairs (16 items
total) appearing only once.
I found that my initial approach to which I postet questions a few days ago
lead to a dead end.
So I used a simpler way tro create those 20 sublists as posted more
recently.
I am still working on a way to change the sequence of items in those 20
sublists til the condition concerning the pairs of items is met.
I know that my goal is mathematically possible with 16 items in 20 sublists
from a discussion a couple of years ago.
I refined my code below but with changing the indices of f1, f2, f3, f4 and
by n manually I'am unable to avoid double pairings.
I think I reached another dead end.
Bur mybe someone could give me an idea for a different approach before
giving up.
>comb_list = []
f>or dummy_i in range(4):
>copy_all_letters = all_letters[:]
>lst = (16*16)*copy_all_letters
>f1 = []
>f2 = []
>f3 = []
>f4 = []
> f1.append(lst[0+n])
>f1.append(lst[2+n])
>f1.append(lst[4+n])
>f1.append(lst[6+n])
> f1.sort()
>comb_list.append(f1)
>f2.append(lst[8+n])
>f2.append(lst[10+n])
>f2.append(lst[12+n])
>f2.append(lst[14+n])
>f2.sort()
>comb_list.append(f2)
> f3.append(lst[1+n])
>f3.append(lst[3+n])
>f3.append(lst[5+n])
>f3.append(lst[7+n])
>f3.sort()
>comb_list.append(f3)
> f4.append(lst[9+n])
>f4.append(lst[11+n])
> f4.append(lst[13+n])
> f4.append(lst[15+n])
>f4.sort()
> comb_list.append(f4)
> print(f1, f2, f4, f4)
> print(comb_list)
> n += 3
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