les_iterables.searching module

Summary

Functions:

duplicates

Find duplicate items.

first_matching

The first item matching a predicate.

nth_matching

The nth item matching a predicate.

Reference

les_iterables.searching.nth_matching(iterable, predicate, n)[source]

The nth item matching a predicate.

Parameters:
  • iterable – An iterable series of items to be searched.

  • predicate – A callable to which each item will be passed in turn.

  • n – A zero-based index.

Returns:

The nth item for which the predicate returns True.

Raises:

ValueError – If there are no matching items.

les_iterables.searching.first_matching(iterable, predicate)[source]

The first item matching a predicate.

Parameters:
  • iterable – An iterable series of items to be searched.

  • predicate – A callable to which each item will be passed in turn.

Returns:

The first item for which the predicate returns True.

Raises:

ValueError – If there are no matching items.

les_iterables.searching.duplicates(iterable, key=None)[source]

Find duplicate items.

[1, 3, 6, 3, 6, 2, 9, 3] -> [3, 6, 3]

Parameters:
  • iterable – The items to be searched.

  • key – An optional function used to generate a key by which items will be compared. If not provided the items themselves will be compared. If the key function returns hashable objects the performance of this function will be O(n); however, the performance will degrade to O(n²) when the first non-hashable key is encountered.

Yields:

Items which are considered duplicates according to the key, in the order that they are encountered. Items which are encountered more than twice will be yielded more than once.