By spapadop


2019-11-08 14:52:47 8 Comments

Right now I'm using a tuple with start and end index:

ind_range = (34,98) 

but what I want is:

ind_range = (34:98)

so I can pass this directly to the array:

array[ind_range]

Edit:

Original question answered: use range()

Ultimately, what I'm trying to do is index another array with a list of index ranges.

ind_ranges=[range(5,9), range(13,19)]
array[ind_ranges]
output: [[values from range1],[values from range2]]

When I do this I get the error:

IndexError: arrays used as indices must be of integer (or boolean) type

Final Edit:

Correct answer: use a slice object in combination with a list comprehension.

3 comments

@Nicolas Gervais 2019-11-08 14:55:25

Literally just array[range(34, 98)].

Since you're all struggling, here's a step by step tutorial:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

print(arr[range(2, 4)])

Out[1]: [3 4]

@spapadop 2019-11-08 14:56:51

just had that epiphany :face_palm: thank you

@spapadop 2019-11-08 14:59:30

ok 2nd question. when I try to index with a list of ranges, I get this issue: IndexError: arrays used as indices must be of integer (or boolean) type

@Matthew Barlowe 2019-11-08 15:07:26

This answer isn't correct i didn't downvote but please update

@Nicolas Gervais 2019-11-08 15:09:16

what's wrong with it? check update, it works.

@Matthew Barlowe 2019-11-08 15:12:41

numpy arrays and lists are two different things

@Nicolas Gervais 2019-11-08 15:13:53

OP said multiple times that he is working with an array, and not a list.

@Yann Vernier 2019-11-08 15:38:09

FYI, Python has arrays without numpy, which don't behave much like numpy's ndarray.

@Tim Körner 2019-11-08 14:59:13

You can just use a slice object.

s = slice(34, 98)

Now you can just use

array[s]

Use a list comprehension if you want to use multiple slices:

slices= [slice(34, 98), slice(34, 98), slice(34, 98)]
values = [array[s] for s in slices]

@spapadop 2019-11-08 15:11:39

the list comprehension part is exactly what I needed, thanks!!!

@user123091203910 2019-11-08 14:58:50

This works for me - Nicolas' solution returns TypeError: list indices must be integers or slices, not range for me in python 3

>>> arr = [i for i in range(15)]
>>> start, end = 8, 12
>>> arr[start:end]
[8, 9, 10, 11]

Related Questions

Sponsored Content

9 Answered Questions

[SOLVED] How can I represent an infinite number in Python?

33 Answered Questions

[SOLVED] Check if a Bash array contains a value

  • 2010-09-10 15:31:33
  • Paolo Tedesco
  • 462954 View
  • 381 Score
  • 33 Answer
  • Tags:   arrays bash

5 Answered Questions

[SOLVED] Python - IndexError: tuple index out of range when using py2exe

  • 2017-01-10 21:32:55
  • Dennis
  • 59853 View
  • 84 Score
  • 5 Answer
  • Tags:   python py2exe

9 Answered Questions

[SOLVED] How to pass an array within a query string?

43 Answered Questions

[SOLVED] How can I represent an 'Enum' in Python?

9 Answered Questions

[SOLVED] Representing and solving a maze given an image

1 Answered Questions

1 Answered Questions

2 Answered Questions

[SOLVED] Indexing a Boolean Array with another Boolean Array in Python

Sponsored Content