Если во время итерации нужно получить доступ к соседним элементам, можно создать итератор, который сделает это автоматически.
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.
Если во время итерации нужно получить доступ к соседним элементам, можно создать итератор, который сделает это автоматически.
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.
Start with a fresh view of investing strategy. The combination of risks and fads this quarter looks to be topping. That means the future is ready to move in.Likely, there will not be a wholesale shift. Company actions will aim to benefit from economic growth, inflationary pressures and a return of market-determined interest rates. In turn, all of that should drive the stock market and investment returns higher.
What is Telegram?
Telegram is a cloud-based instant messaging service that has been making rounds as a popular option for those who wish to keep their messages secure. Telegram boasts a collection of different features, but it’s best known for its ability to secure messages and media by encrypting them during transit; this prevents third-parties from snooping on messages easily. Let’s take a look at what Telegram can do and why you might want to use it.