PYTHONPORTAL Telegram 4412
Функция consume() из itertools — это способ вычитать все элементы итератора, не сохраняя их.

Но способов реализовать это гораздо больше, и вот самые интересные

> Через for:

def consume(iterable):
for _ in iterable:
pass


> С builtins:

set(map(type, zip(iterable)))
min(map(bool, zip(iterable)))


> Через collections:


from collections import deque
deque(iterable, maxlen=0)


> Через itertools:

next(compress(iterable, repeat(False)), None)
next(dropwhile(id, iterable), None)
next(islice(iterable, sys.maxsize, None), None)

iterator = groupby(iterable, key=lambda x: None)
next(iterator, None)
next(iterator, None)


Все эти трюки делают одно — вычищают итератор. Полезно в бенчмарках, ленивых вычислениях

Кто знал хотя бы 3 из них? 🤔

👉 @PythonPortal
Please open Telegram to view this post
VIEW IN TELEGRAM
👍265



tgoop.com/PythonPortal/4412
Create:
Last Update:

Функция consume() из itertools — это способ вычитать все элементы итератора, не сохраняя их.

Но способов реализовать это гораздо больше, и вот самые интересные

> Через for:

def consume(iterable):
for _ in iterable:
pass


> С builtins:

set(map(type, zip(iterable)))
min(map(bool, zip(iterable)))


> Через collections:


from collections import deque
deque(iterable, maxlen=0)


> Через itertools:

next(compress(iterable, repeat(False)), None)
next(dropwhile(id, iterable), None)
next(islice(iterable, sys.maxsize, None), None)

iterator = groupby(iterable, key=lambda x: None)
next(iterator, None)
next(iterator, None)


Все эти трюки делают одно — вычищают итератор. Полезно в бенчмарках, ленивых вычислениях

Кто знал хотя бы 3 из них? 🤔

👉 @PythonPortal

BY Python Portal


Share with your friend now:
tgoop.com/PythonPortal/4412

View MORE
Open in Telegram


Telegram News

Date: |

The court said the defendant had also incited people to commit public nuisance, with messages calling on them to take part in rallies and demonstrations including at Hong Kong International Airport, to block roads and to paralyse the public transportation system. Various forms of protest promoted on the messaging platform included general strikes, lunchtime protests and silent sit-ins. To delete a channel with over 1,000 subscribers, you need to contact user support The group’s featured image is of a Pepe frog yelling, often referred to as the “REEEEEEE” meme. Pepe the Frog was created back in 2005 by Matt Furie and has since become an internet symbol for meme culture and “degen” culture. Select “New Channel” Joined by Telegram's representative in Brazil, Alan Campos, Perekopsky noted the platform was unable to cater to some of the TSE requests due to the company's operational setup. But Perekopsky added that these requests could be studied for future implementation.
from us


Telegram Python Portal
FROM American