I was not happy with any of these approaches, so I came up with the Flexlist class, which allows you to flexibly index either an integer, a slice, or an index list:
class Flexlist(list): def __getitem__(self, keys): if isinstance(keys, (int, slice)): return list.__getitem__(self, keys) return [self[k] for k in keys]
What for your example would you use as:
L = Flexlist(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) Idx = [0, 3, 7] T = L[ Idx ] print(T) # ['a', 'd', 'h']
jedwards Apr 04 '15 at 2:59 a.m. 2015-04-04 14:59
source share