0

I have two 1-D arrays:

tminus = [219 220 225 226 227 332]
tplus = [221 222 227 228 229 334]

And a 2-D array:

t = [[222 224 228 244],[264 280 283 255 346]]

How do I append t for the values that are in between tminus and tplus? I am trying to keep t as the 2-D array. I tried:

time = []
for k in range(len(tminus)):
    for i in range(len(t)):
        for j in range(len(t[i])):
            if tminus[k] <= t[j] <= tplus[k]:
                time.append(t[j])
print time

But, all I get is the empty list.

Any suggestions?

3
  • Do you want the values of t to be between the minimum of tminus and the maximum of tplus? Commented Feb 2, 2019 at 18:56
  • So for every value of t, I want to see if that one value is in between the values of tminus and tplus. So for t[0][0], I want to see if its between tminus[0] and tplus[0] and then if its in between tminus[1] and tplus[1] etc. and if it is then, the value for t is appended into time. Commented Feb 2, 2019 at 19:09
  • what result do you expect from the given inputs? Commented Feb 2, 2019 at 19:58

2 Answers 2

1

In this line:

if tminus[k] <= t[j] <= tplus[k]:

Note that you call t[j], which will find the jth element inside of t. However, t[j] will always be a list, as t is a 2D array.

To iterate over the sublists(t[i]) inside of t, use

t[i][j]

This gets the jth element in the ith element in t.

For example, if i is 0, and j is also 0, t[j] would return [222, 224, 228, 244], and you can't compare an int to a list. Instead, t[i][j] would return 222, which is what you were aiming for.

Because you also want the shape of t to stay the same, create a temporary variable (appendList), append values to that, then append the whole list to t.

However, with your current code, it will create unnecessary lists, as it will create one for every element in tminus. To avoid this, you can switch the order of the for loops, so that it only creates appendList. Also, this can cause a single number to be appended multiple times, so we pass it into set() to remove duplicates, and then pass it into list() to turn it into a list again.

Updated code:

tminus = [219, 220, 225, 226, 227, 332]
tplus = [221, 222, 227, 228, 229, 334]
t = [[222, 224, 228, 244],[264, 280, 283, 255, 346]]

time = []
for i in range(len(t)): # Iterate over t
    appendList = [] # Stores values to be appended to time
    for j in range(len(t[i])): # Iterate over the sublists in t
        for k in range(len(tminus)): # Iterate over tminus
            if tminus[k] <= t[i][j] <= tplus[k]: # Check value
                appendList.append(t[i][j]) # Append value to appendList
    appendList = list(set(appendList)) # Remove duplicates
    time.append(appendList) # Append entire list to time
print(time)

This gives the result: [[228, 222], []]

Caveats:
- If t has repeating elements, only one of them will stay
- Order is lost when passing into set()

Sign up to request clarification or add additional context in comments.

1 Comment

but is the way I wrote the loop the correct way to write it? For every value of t, I want to see if that one value is in between the values of tminus and tplus. So for t[0][0], I want to see if its between tminus[0] and tplus[0] and then if its in between tminus[1] and tplus[1] etc. and if it is then, the value for t is appended into time. I would like the t array to still be 2-D
0

I am not really sure if this is what you are looking for but from my understanding this should solve the problem.

index = 0
for i in range(0,len(t)):
    for j in range(0,len(t[index])):
        if max(tminus) <= t[i][j] <= min(tplus):
            time.append(t[i][j])
    index +=1
return time

Comments

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.