'slice()' In Python Is Underrated

preview_player
Показать описание
Is "slice()" in Python underrated? I personally never had to use it until I started working with Pandas. What I like about it is that it promotes code reusability, even though I find it uncommon to have to repeat the same slice throughout a project.

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

slice is fantastic. I have a complicated space instrument with around 500 channels, and it comes in so fast the electronics can't keep up, so there are 4 separate amplifier chains...which are all identical. but of course they aren't, so each needs to be calibrated separately, The guys were doing it in Matlab with handwritten if chain=='north': idx = [1, 5, ...] else./...repeated everywhere.

So I enlightened them (oh, and it had a time stamp in position 0), so I just made a dictionary of slice object vs amplifier chain (named after the cardinal directions):

AMP = dict(time=slice(0, 1), north=slice(1, None, 4), east=slice(2, None, 4), south=(3, None, 4), west=(4, None, 4))

so a 

calibration = {amp: sensor.data[AMP[amp]].mean() for amp in ('north', 'east', 'south', 'west')}


gave me a dictionary of time stamped calibration sample averages (when the system was in calibration mode). Then you save that and scale environmental data until the next cal measurment 2 seconds later.

`No ifs, no loops. cyclomatic complexity minimized. Nothing is WETT (write everything twice), it's all DRY (don't repeat yourself).

DrDeuteron
Автор

Ya much better example than the short video, now I get the importance of it! Thank you for sharing the awesome tip.

MFM
Автор

your setdefault video was quite usefull, I wouldn't say this is so much, but it's good to know might come in handy perhaps. Its nice to know small tools like this

Sinke_
Автор

Honestly, I've never had to do this either, but if I ever had to, I would just use a variable to control it.

x = 3
print(text[x:])
print(text[x:])

anubis
Автор

It looks useful, I love to know that it exists. But as you said I have never really needed it haha Thanks for the video!

cristobaljvp
Автор

If you're going to write a full expression as a slice, this is definitely useful.

RedHair
Автор

I like that you changed the example to this one since the short.
Personally i never used the slices, but it does look useful in certain cases, like comparing the first/last n character of a string with each other for some reason.
One usecase can be like passing the slice to a function, or make it a variable so you can change it inside a class

There are way too many tricks in python to like ever run out of content or ever feel like one knows everything that the language can offer.

ProfRoxas
Автор

Praised to you bro, you taught as easy the way you taught a python

paulnigelressurrecion
Автор

2:17 The None isn't representing the colon, the comma is representing the colon.

E.g.
slice(None, None) is equivalent to [:], not [::]

phizc
Автор

by putting variables into the string slicing brackets, doesn't this give the same functionality as using slice()?? and then just update the variables??

text: str = 'I am a teapot'
text2: str = 'I am not a teapot'

a = 3
b = 10

print(text[a:b])
print(text2[a:b])


a = 5
b = 8

print(text[a:b])
print(text2[a:b])

mpb
Автор

The only use case I can think of is having a text file with fixed-width fields. Using named slices to identify each field may make the code more readable, especially if there is no need to separate the line into its various fields.

apmcd
Автор

I needed this or something like this years ago when I was just getting started. I wonder if I could still use it or if I'd just approach the problem completely differently now

korntut
Автор

Could you please make a series on DSA..

yasink
Автор

You are awesome!
Mukesh Kumar (Indian)

mukkum
Автор

is it make a shallow copy of the original object ? did anyone test that ?

xzex
Автор

I'd be interested if this is compatible with other libraries that use slicing quite heavily such as numpy and pandas.

zachcheung
Автор

Sir please give a full topic video on class and objects
I am having a exam to join a company, it would help me a lot
Please 🙏🙏

yasinsk
Автор

In short you said in pandas you really had to use it, you could have gived example where it's really nessesary

Sinke_
Автор

I mean...
Cannot we just do this instead?
text = "I am a teapot"
text2 = "I am not a Teapot"

slice_str = "[3:10]"
parsed_slice = slice(*map(int, slice_str[1:-1].split(":")))

result1 = text[parsed_slice]
result2 = text2[parsed_slice]

print(result1)
print(result2)

dragonbone