[Tutor] problem solving with lists

marcus.luetolf at bluewin.ch marcus.luetolf at bluewin.ch
Sat Mar 19 09:55:31 EDT 2022


First to dn’s answer:

I am not an professional programmer. Programming with pyhon is a hobby in the sense of life long learning.

And other than a course in C I have no knowledge of other programming languages. I am just trying to solve

my problem with what little I know about python. That’s why I turned to tutor for help. I have found an

algorithm and all required code steps but the very last one,  admittedly helped by most wellcome

comments from tutor.

 

dn’s « definitions » are correct but I reduced the problem solving to

>all_letters = ‘abcdefghi’

>number_of_lists = 4

>number_of_sublists per list = 3

>number_per_sublist = 3

 

But dn’s «results obtained»  is not what I need, I replaced them by itertools function combinations.

 

dn’s last section I don’t understand.

Second to Alan G.’s replay :

My problem ist to create a set of unique sublists of 3 items/letters out of a list of 9 items/letters, later to be extended to 

sublists of 4 items/letters out of a list of 16 items/letters.

I am using letters as items for the sake of readability. Once the code works letters will be substituted by names of people 

for the final goal is to set up  «unique» teams.

 

«Unique» means that other than single items/letters,  pairs of items/letters (pair1, pair2, pair3) can appear only once in

all sublists. A sublist could be understood as a combination of pair1+pair2+pair3. At the end there should be only 12 sublists

in 4 lists left.

 

Sorry for the length of this replay, Marcus.

 

 

Von: alan.gauld at yahoo.co.uk <alan.gauld at yahoo.co.uk> 
Gesendet: Samstag, 19. März 2022 13:25
An: marcus.luetolf at bluewin.ch
Cc: tutor at python.org
Betreff: Re: AW: AW: AW: [Tutor] problem solving with lists

 

 

 

On 19 Mar 2022 10:57, marcus.luetolf at bluewin.ch <mailto:marcus.luetolf at bluewin.ch>  wrote:

Many thanks again.

Using your first advice my code got even clearer :

 

>lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'g'], ['b', 'c', 'h']]

>sub_lst = []

 

>for p in lst:

>    pair1 = p[0:2]

>    pair2 = list(p[0]+ p[2])

>    pair3 = p[1:] 

>    if pair1 not in sub_lst or pair2 not in sub_lst or pair3 not in sub_lst:

>        sub_lst.append(p)

>print(sub_lst)

 

But your second advice :

>if pair1 not in sub_lst or pair2 not in sub_lst or pair3 not in sub_lst: 

 

Still yields all four sublists instead of eliminating the last 2 sublists , ['a', 'b', 'g'], ['b', 'c', 'h'],

since ‘a’, ‘b’ and ‘b’, ’c’ were already in the first 2 sublists.

I think python does not allow to check if an item is in a list with only slices of the item to be checked ??

Python does what you ask. If you get the wrong answer then you are asking the wrong question. 

 

But I still don't really understand what you are trying to do. Have you looked at Dennis replies, his approach is more likely to get you what you want.... 

 

Alan g

Von: alan.gauld at yahoo.co.uk <mailto:alan.gauld at yahoo.co.uk>  <alan.gauld at yahoo.co.uk <mailto:alan.gauld at yahoo.co.uk> > 
Gesendet: Freitag, 18. März 2022 19:16
An: marcus.luetolf at bluewin.ch <mailto:marcus.luetolf at bluewin.ch> 
Cc: tutor at python.org <mailto:tutor at python.org> 
Betreff: Re: AW: AW: [Tutor] problem solving with lists

 

 

 

On 18 Mar 2022 15:12, marcus.luetolf at bluewin.ch <mailto:marcus.luetolf at bluewin.ch>  wrote:

…many thanks for your quick replay.

When I used a for loop :

 

>lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'g'], ['b', 'c', 'h']]

>sub_lst = []

>for p in lst:

>    pair1 = lst[p][0:2]

 

 

P is not an index its the actual sublist. So you just need

 

Pair1 = p[0:2]

 

 

> 

> ected. But I don’t understand

what you mean with


If pair 1 not empty

Or pair2 not empty

Or pair3 not in newlist

 

What is the difference between pair not beeing empty or not beeing in new_list ?

 

Pair not empty means pair contains anything other than the empty list.

Pair not in newlist means pair is not empty and it's contents are not I. Newlist. Very different! 

How should I define the if condition properly ?

 

 

You need, I assume, 

 

If Pair1 not in newlist 

Or pair2 not in newlist

Or pair3 not in newlist

 

Alan g. 

 

 



More information about the Tutor mailing list