multiplying str / list / tuple and pitfalls (beginner - intermediate) anthony explains #361

preview_player
Показать описание
today I talk about some nifty operator overloading in python for multiplication and why you have to be careful with it sometimes!

==========

I won't ask for subscriptions / likes / comments in videos but it really helps the channel. If you have any suggestions or things you'd like to see please comment below!
Рекомендации по теме
Комментарии
Автор

little optimisation trick is to pre-allocate lists when you need to fill them with random values (vs append)
or use comprehensions (sometimes not suitable)
(in order to prevent list resize when it full)

%%timeit
x = [0] * n
for i in range(n): x[i] = random.random()
975 ms ± 9.71 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%%timeit
x = [random.random() for i in range(n)]
913 ms ± 24.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%%timeit
x = []
for i in range(n): x.append(random.random())
1.14 s ± 10.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
(not so much but slower because of list's array resize under the hood)

tandavme
Автор

Keep Going My Friend On The Internet 😎👍

bi
Автор

Remember one of my first problem solving in Python and got cought by exact problem with multiplying list of objects. I initialize table for board puzzle of sort and do comprehension inside and the multiplication.

It was pain to debug that. Great video as always.

marsma
Автор

Thanks for another nice video! A video topic request: How pip's cache works / pip decides what to cache. I was surprised to learn today that in my pip cache I have 28 JAX wheels but no NumPy or SciPy wheels (I use all 3 extensively across many virtual environments).

$ find ~/.cache/pip/ -iname "jax*" | wc -l
28
$ find ~/.cache/pip/ -iname "numpy*" | wc -l
0

I understand that this may be niche enough to not be interesting enough for a video.

mfeickert
Автор

Don't mind me. Just here to add a like for the algorithm Gods to see

ItzAnameOk