0

The Vals list comprehension below modifies Values such that for the number of nth rows it indexes the array values as such. How would I be able to add an increment to the Vals list comprehension where it adds 100 in front of all of the modified lists? I want to only modify the list comprehension function to do that.

import numpy as np 

first_index_val = 100
Values = np.array([[130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72]])

Vals = np.array([arr[i:] for i,arr in enumerate(Values.tolist())])

Output:

[list([130.0, 123.0, 135.3, 139.05, 156.08, 163.88, 173.72])
 list([123.0, 135.3, 139.05, 156.08, 163.88, 173.72])
 list([135.3, 139.05, 156.08, 163.88, 173.72])
 list([139.05, 156.08, 163.88, 173.72]) list([156.08, 163.88, 173.72])
 list([163.88, 173.72]) list([173.72])]

Expected Output:

[list([100, 130.0, 123.0, 135.3, 139.05, 156.08, 163.88, 173.72])
 list([100, 123.0, 135.3, 139.05, 156.08, 163.88, 173.72])
 list([100, 135.3, 139.05, 156.08, 163.88, 173.72])
 list([100, 139.05, 156.08, 163.88, 173.72]) list([100, 156.08, 163.88, 173.72])
 list([100, 163.88, 173.72]) list([100, 173.72])]
1
  • Just use [100] + arr[i:] in the list comprehension` Commented Jan 9, 2022 at 22:50

2 Answers 2

1

Here is my take on it, simple and declarative.

import numpy as np

first_index_val = 100
values = np.array([[130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72]])

values = np.array([ [100] + arr[i:] for i, arr in enumerate(values.tolist())])
print(values)
Sign up to request clarification or add additional context in comments.

4 Comments

use your enumeration after the last flip
Thanks for the solution but I still dont get the Expected Output when doing that. Even after the enumeration.
In that case, follow the solution posted by @Pepsi-Joe, only he misplaced the [100] it should be: np.array([ [100] + arr[i:] for i, arr in enumerate(values.tolist())])
I edited the code above to get the solution you want. Hope this helps.
0

Just add in the addition to the list comprehension.

Vals = np.array([[100] + arr[i:] for i,arr in enumerate(Values.tolist())])

2 Comments

the functionality for this code does not work
Yes, it seems that Numpy is not happy with having rows of different lengths. Do you need it in an array? it seems you want a list of lists. If you are okay with a list of lists, then just get rid of np.array in my answer.

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.