Skip to content

Instantly share code, notes, and snippets.

@colematt
Created November 4, 2023 01:54
Show Gist options
  • Save colematt/eaf68e1c35da939cb1709f57565c14d6 to your computer and use it in GitHub Desktop.
Save colematt/eaf68e1c35da939cb1709f57565c14d6 to your computer and use it in GitHub Desktop.
Array Recipes

Get elements from a multi-dimensional array

import functools
import operator
def arrget(array,tup):
	'''
	Given a tuple containing the dimensional indices, retrieve that element from a multi-dimensional array
	'''
	return functools.reduce(lambda arr, idx: operator.getitem(arr,idx), tup, array)

Transpose an array

import itertools
def transpose(array):
	T = type(array)
	return T(itertools.zip_longest(*array))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment