Sujet : Re: Two python issues
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 06. Nov 2024, 02:26:51
Autres entêtes
Organisation : Stefan Ram
Message-ID : <two-20241106011638@ram.dialup.fu-berlin.de>
References : 1
Raymond Boute <
raymond.boute@pandora.be> wrote or quoted:
s[s.find('a')]
'p'
If you want exceptions, index() is your jam.
Moreover, using index -1 for the last item is a bad choice: it should be
len(s) - 1 (no laziness!).
This feature is the bee's knees, man.
last_item = my_list[ -1 ]
second_to_last = my_list[ -2 ]
Way cleaner than my_list[ len( my_list )- 1 ], don't you think?
(b) When using assignment for slices, only lists with the same length as
the slice should be acceptable, otherwise an error should be given.
The current setup lets you pull off some seriously gnarly code
maneuvers:
python
# Insert elements
my_list[ 2: 2 ]=[ 4, 5, 6 ]
# Replace a section
my_list[ 1: 4 ]=[ 10, 11 ]
# Delete elements
my_list[ 2: 5 ]= []
These moves are slick and expressive. Forcing exact length
matching would only cramp our style.
Look, I get it. You're probably fresh out of coding bootcamp,
thinking you can reinvent the wheel. But trust me, these features
are solid gold. They're not perfect, sure, but they're about
as good as finding a parking spot in San Francisco on a Saturday
night. So before you go throwing shade at Python, maybe spend
some more time with it. You might just find it grows on you like
a NorCal redwood. Keep coding, and don't let the fog get you down!