Repository with sources and generator of https://larlet.fr/david/ https://larlet.fr/david/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.md 1.2KB

title: Voisinage en Python

J’ai besoin de connaître les articles précédents et suivants pour le blog et maintenant pour ces notes, je suis arrivé à la version suivante en Python qui produit un générateur à partir d’un itérable grâce au mot-clé yield :

:::python
def neighborhood(iterable, first=None, last=None):
    """
    Yield the (previous, current, next) items given an iterable.

    You can specify a `first` and/or `last` item for bounds.
    """
    iterator = iter(iterable)
    previous = first
    current = iterator.next()  # Throws StopIteration if empty.
    for next in iterator:
        yield (previous, current, next)
        previous = current
        current = next
    yield (previous, current, last)

Il me fallait surtout la possibilité de spécifier un premier/dernier item pour avoir un lien sur le premier article publié vers les pensées plus anciennes ou la première note vers les tweets archivés. Il y a une suite et une fin à cette série.