1

I am not very used to pyhton and want to write a function that takes an array x as an input and gives an array back (select) consisting only of those entries of the input array that fulfil a certain property, e.g. being in a certain range. The function that should do this is the following:

def select(x):
    count = 0
    select = []                #0
    for i in range(0,len(x[0])):
      if ( (int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5)  ):
        select[0][count]=x[0][i] #1
        select[1][count]=x[1][i] #2
        select[2][count]=x[4][i] #3
        count = count + 1
    return select

However, if I call that function I get the following error message:

IndexError: list index out of range

the line causing it is "#1" (and the 2 following lines are making trouble too I think). I guess I have to define the array size in some way. How can I do that in python in that case? As I see select=[] is not enough.

Kind regards

1
  • The issue (as I think you realize) is select has no [0] element. Can you show what you expect the input x and output select to be for some case? Commented Jan 13, 2016 at 14:03

6 Answers 6

1

select is an empty list initially. You are trying trying to assign values to its elements that don't currently exist.

Probably you need select = [[], [], []]

And elements of inner elements of the select won't also exist when you try to assign them in #1, #2 and #3

Perhaps this is what you want:

def select(x):
    select = [[] for i in range(3)]    #0 : [[], [], []]
    for i in range(0,len(x[0])):
      if ( (int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5)  ):
        select[0].append(x[0][i]) #1
        select[1].append(x[1][i]) #2
        select[2].append(x[4][i]) #3
    return select
Sign up to request clarification or add additional context in comments.

4 Comments

Perhaps, but seems like an odd choice to blank fill it with. I think select = range(3) is the more pythonic way.
He wants to nest something within select, so a dict or another list is required, not a range.
Totally, gotcha, duh!
@jatinderjit Thanks a lot!
1

As an alternative to your own filter method (to achieve array filtering w.r.t. a range), you can use the following approach

myRange = [8, 11]
myArr = [6, 7, 8, 9, 10, 11, 12]
myArrFiltered = [x for x in myArr if myRange[0] <= x <= myRange[1]]
print(myArrFiltered)
# [8, 9, 10, 11]

Comments

1

Your code does not work because select list has no items at index 0.

If I were to implement similar functionality I would have used filter with a function that decides which elements should be selected:

ages = [5, 15, 3, 7, 18, 34, 9, 10]

def check_age(age):
    # we could implement all sorts of logic here
    # if we return True, the element is selected
    return age > 10

children_over_ten = filter(check_age, ages)

print(children_over_ten)

Or simply use list comprehension if the selection criteria is simple enough:

ages = [5, 15, 3, 7, 18, 34, 9, 10]
children_over_ten = [x for x in ages if x > 10]
print(children_over_ten)

Comments

1

You should probably try using list comprehensions, which can be used for filtering data.

For example with interactive python session,

>>> data = [i*0.25 for i in range(0, 21)]
>>> data
[0.0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 4.0, 4.25, 4.5, 4.75, 5.0]
>>> [x for x in data if abs(2.5 - x) <= 0.5]
[2.0, 2.25, 2.5, 2.75, 3.0]

which selects the data that is in range [2.0, 3.0].

Iterating through an array can be often accomplished without using indexes, for example

for x in array:
    print(x)

Comments

1

Or declare select = [], and do it this way:

def select(x):
    count = 0
    select = [] #0
    for i in range(0,len(x[0])):
      if ( (int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5)  ):
        row = [x[r*r][i] for r in range(3)]
        select.append(row)
        count = count + 1
    return select

Comments

1

You need to initialize select accordingly

def select(x):
    count = 0
    select = [[],[],[]]                #0
    for i in range(0,len(x[0])):
      if ( (int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5)  ):
        select[0]+=[x[0][i]] #1
        select[1]+=[x[1][i]] #2
        select[2]+=[x[4][i]] #3
        count = count + 1
    return select

3 Comments

thanks for your time, following this I get TypeError: You must first set_array for mappable, any ideas? To be honest I don't know what += does.
a += b is a shorthan for a = a+b. For list, + does the concatenation.
@DonkeyKong, I just edited the question I did not make the right hand side a list. I just added [] arround the rhs. You can also use append instead of the concatenation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.