[Tutor] problem solving with lists
Dennis Lee Bieber
wlfraed at ix.netcom.com
Fri Mar 18 11:34:27 EDT 2022
On Fri, 18 Mar 2022 14:07:00 +0100, <marcus.luetolf at bluewin.ch> declaimed
the following:
>
>>lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'g'], ['b', 'c', 'h']]
>
>>n = len(lst)
>>p = 0
>>new_lst = []
>>while n > 0:
>> pair1 = lst[p][0:2]
>> pair2 = list(lst[p][0]+ lst[p][2])
>> pair3 = lst[p][1:]
ONE: this is not scaleable... You are hard-coded to working with
elements of exactly length three...
TWO: your definition of pair2 seems to imply that pairs are NOT
adjacent... Your examples over the course of this thread have never
provided an answer to that question -- even after having been asked
multiple times.
If pairs are not required to be adjacent letters, then testing with
"in" will not suffice. Set intersection is the best solution for disjoint
pairs.
>> print('pair1:', pair1, 'pair2: ', pair2, 'pair3: ', pair3)
>> if pair1 or pair2 or pair3 not in new_lst:
This does not do anything like you think it does. It is parsed as
if (pair1)
OR (pair2)
OR (pair3 not in new_lst)
and since a non-empty list is "true", "pair1" by itself determines the
condition and pair2/pair3 aren't even looked at...
https://docs.python.org/3/reference/expressions.html#membership-test-operations
https://docs.python.org/3/reference/expressions.html#boolean-operations
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list