0

Names such as map, filter or sum are generally understood by every resonably good programmer.

I wonder whether the following function f also has such a standard name:

def f(data, idx): return [data[i] for i in idx]

Example usages:

r = f(['world', '!', 'hello'], [2, 0, 1, 1, 1])

piecePrice = [100, 50, 20, 180]
pieceIdx = [0, 2, 3, 0, 0]
total Price = sum(f(piecePrice, pieceIdx))

I started with map, but map is generally understood as a function that applies a function on each element of a list.

Michael
  • 7,407
  • 8
  • 41
  • 84
  • 3
    If `idx` were a contiguous sequence of consecutive integers, I might call it a slice or substring or subsequence. If the number of indices in `idx` were equal to the number of elements in `data` (with no repeats in `idx`) I'd call it a permutation or a shuffle. Otherwise, most generally, I'd call it a multi-subset. I guess what I'm trying to say is that the specifics about what `idx` might bear on what it's called in a particular context. – Patrick87 Apr 03 '19 at 12:09
  • 1
    I'd call that `select` – Matt Timmermans Apr 03 '19 at 12:43
  • 1
    In NumPy it's called [`take`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.take.html) – Georgy Apr 03 '19 at 13:25
  • There is also [`itemgetter`](https://stackoverflow.com/a/18272249/7851470) but it works differently. – Georgy Apr 03 '19 at 13:37

0 Answers0