What "reversed()" ACTUALLY Does In Python

What "reversed()" ACTUALLY Does In Python

Indently

1 год назад

10,282 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Timur P
Timur P - 21.07.2023 20:08

Hello, I was trying to find your old video about autoresponder for Instagram in Python, but it is deleted. Could you please send me this video(if it is possible). tysm

Ответить
Dr Gamma D
Dr Gamma D - 20.07.2023 16:41

I don’t think I like type hints on assignment.

Ответить
Lol52
Lol52 - 17.07.2023 17:38

i think many people get confused by list.reverse into thinking there is one for string too

Ответить
This Old Property
This Old Property - 17.07.2023 06:07

Recently saw a presentation on distributed computing in python using 'ray'. Thought you might like to know.

Ответить
Pawlo 370
Pawlo 370 - 16.07.2023 19:09

you can change size without changing data
from sys import getsizeof
tablica = [i+1 for i in range(1_000_000)]
print(f"Rozmiar pliku wynosi: {getsizeof(tablica)} bajtów.")
tablica = list(reversed(list(reversed(tablica))))
print(f"Rozmiar pliku wynosi: {getsizeof(tablica)} bajtów.")

Ответить
ADITYA MISHRA SC257
ADITYA MISHRA SC257 - 16.07.2023 04:34

Got it, I knew 'reversed()' returns reversed object, but didn't knew it was memory efficient and exhaustive.

Ответить
sk8erJG95
sk8erJG95 - 15.07.2023 17:44

This is a great lesson on the utility of iterators as well! Makes things a lot clearer

Ответить
Kevin Cherry
Kevin Cherry - 15.07.2023 16:39

With only 48 bytes, it must keep a pointer to the original list. This would mean if the list changes a value before you grab it from the iterator, you would get the new changed value. However with strings, since they are immutable, changing the original string means secretly increasing memory usage since the iterator would keep a reference to the original string so it can't be garbage collected. So both the changed and original would reside in memory. I haven't tested this so someone can correct me if I'm wrong, but this seems like the only way this would work with so little memory consumed for the reversed object.

Ответить
berkay bakacak
berkay bakacak - 15.07.2023 15:47

I noticed that you typed the letter 'ç', could you possibly be Turkish? 🤔 😁

Ответить
Sinisa Vlahovic
Sinisa Vlahovic - 15.07.2023 15:06

Same as sorted, generators are nice thing

Ответить
Indently
Indently - 15.07.2023 13:36

You can test it out with:

numbers: list[int] = list(range(1_000_000))
(8000056 bytes)

reversed(numbers)
(48 bytes)

Pretty neat huh?

Ответить