[Tutor] problem solving with lists, coding problems

Dennis Lee Bieber wlfraed at ix.netcom.com
Wed Apr 6 17:10:33 EDT 2022


On Wed, 6 Apr 2022 20:43:14 +0200, <marcus.luetolf at bluewin.ch> declaimed
the following:

>
>>all_players = list('abcdefghijklmnop')  # this variable as a string throws
>errors

	What error? Where?

>>c_all_players = all_players[:]
>


>>> all_players = "abcdefghijklmnop"
>>> c_all_players = all_players[:]
>>> 

No error here... Though I again have to question your reliance on defining
one iterable, only to make a complete copy of the very same iterable.

>>flight_1_day_1 = []
>>flight_2_day_1 = []
>>flight_3_day_1 = []
>>flight_4_day_1 = []
>

	Similarly your use of hard-coded groups here rather than some structure
on which you can perform a loop.

>>for player in c_all_players[0:4]:
>>    flight_1_day_1.append(player)
>>    player_history_a = {player:{player} for player in flight_1_day_1}

	Take a pencil and paper, and run that loop by hand. For one thing, you
have the list comprehension INSIDE the for loop -- and both the list-comp
and the for loop are using the same NAME for their control variables!

	Consider (I changed the control name in the list-comp to make it
explicit)

>>> flight_1_day_1 = []
>>> for player in all_players[0:4]:
... 	flight_1_day_1.append(player)
... 	print(flight_1_day_1)
... 	player_history_a ={plr : {plr} for plr in flight_1_day_1}
... 	print(player_history_a)
... 
['a']
{'a': {'a'}}
['a', 'b']
{'a': {'a'}, 'b': {'b'}}
['a', 'b', 'c']
{'a': {'a'}, 'b': {'b'}, 'c': {'c'}}
['a', 'b', 'c', 'd']
{'a': {'a'}, 'b': {'b'}, 'c': {'c'}, 'd': {'d'}}
>>> 

	All the above is equivalent to...

>>> f1d1 = list(all_players[0:4])
>>> print(f1d1)
['a', 'b', 'c', 'd']
>>> pha = { plr : {plr} for plr in f1d1}
>>> print(pha)
{'a': {'a'}, 'b': {'b'}, 'c': {'c'}, 'd': {'d'}}
>>> 

>>print('player_history_a: ',player_history_a)         
>>del c_all_players[0:4]
>
	The only place I can see that would throw an error is the use of that
"del" statement, as strings are immutable. The effect is equivalent to...

	c_all_players = c_all_players[4:]

	But then, I'd drop even that... You have hard-coded so much already
that does not support looping that you might just as easily hard-code the
substring ranges...

	for player in all_players[0:4]:
...

    
>>for player in c_all_players[0:4]:

	for player in all_players[4:8]:

...repeat with 8:12 and 12:16


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list