BOOKPYTHON Telegram 3685
Если во время итерации нужно получить доступ к соседним элементам, можно создать итератор, который сделает это автоматически.


from itertools import tee

def neighbours(iterable, n):
neighbours = tee(iterable, n)
for i, neighbour in enumerate(neighbours):
for _ in range(i):
next(neighbour)

return zip(*neighbours)

fibb = [1, 1, 2, 3, 5, 8, 13, 21]

for a, b, c in neighbours(fibb, 3):
assert c == a + b


В этом примере мы разветвляем исходный итерируемый объект с помощью tee, затем сдвигаем полученные итераторы с помощью next, чтобы второй начинался со второго элемента исходного итерируемого объекта, а третий — с третьего, и затем объединяем их обратно с помощью zip.

👉@BookPython



tgoop.com/BookPython/3685
Create:
Last Update:

Если во время итерации нужно получить доступ к соседним элементам, можно создать итератор, который сделает это автоматически.


from itertools import tee

def neighbours(iterable, n):
neighbours = tee(iterable, n)
for i, neighbour in enumerate(neighbours):
for _ in range(i):
next(neighbour)

return zip(*neighbours)

fibb = [1, 1, 2, 3, 5, 8, 13, 21]

for a, b, c in neighbours(fibb, 3):
assert c == a + b


В этом примере мы разветвляем исходный итерируемый объект с помощью tee, затем сдвигаем полученные итераторы с помощью next, чтобы второй начинался со второго элемента исходного итерируемого объекта, а третий — с третьего, и затем объединяем их обратно с помощью zip.

👉@BookPython

BY Библиотека Python разработчика | Книги по питону


Share with your friend now:
tgoop.com/BookPython/3685

View MORE
Open in Telegram


Telegram News

Date: |

Some Telegram Channels content management tips According to media reports, the privacy watchdog was considering “blacklisting” some online platforms that have repeatedly posted doxxing information, with sources saying most messages were shared on Telegram. With the sharp downturn in the crypto market, yelling has become a coping mechanism for many crypto traders. This screaming therapy became popular after the surge of Goblintown Ethereum NFTs at the end of May or early June. Here, holders made incoherent groaning sounds in late-night Twitter spaces. They also role-played as urine-loving Goblin creatures. During the meeting with TSE Minister Edson Fachin, Perekopsky also mentioned the TSE channel on the platform as one of the firm's key success stories. Launched as part of the company's commitments to tackle the spread of fake news in Brazil, the verified channel has attracted more than 184,000 members in less than a month. A Hong Kong protester with a petrol bomb. File photo: Dylan Hollingsworth/HKFP.
from us


Telegram Библиотека Python разработчика | Книги по питону
FROM American