-3

I have a list of 2 different values:

data = [[0,1,2,3,4,5,6,7,8,9], [1514764800, 1514851200, 1514937600, 1515024000, 1515110400, 1515196800, 1515283200, 1515369600, 1515456000,]]

Now I want to create a list from list_of_values[0] based on a condition of list_of_values[1] How could this be done? I tried doing

train_data = [x for x in data[0] if data[1][x] < window_start]

but this does not work.

lets say that window_size will be 1514851201 so my desired output would be [0,1]

2 Answers2

0

There were a couple of mistakes in your code. First of all you were looking for the data in the data list that you had declared as list_of_values, and, secondly, your sample list_of_values had 10 items in list_of_values[0] but only 9 in list_of_values[1]

You can do this:

list_of_values = [[0,1,2,3,4,5,6,7,8], [1514764800, 1514851200, 1514937600, 1515024000, 1515110400, 1515196800, 1515283200, 1515369600, 1515456000]]

window_start = 1514851201

train_data = [x for x in list_of_values[0] if list_of_values[1][x] < window_start]

print(train_data)

The output will be:

[0, 1]
lorenzozane
  • 1,214
  • 1
  • 5
  • 15
  • You removed the `9` from the first list, which causes an error. – Haytam Nov 19 '20 at 12:02
  • @Haytam Yes, I was going to specify that by editing the answer – lorenzozane Nov 19 '20 at 12:03
  • Note that the OP's error is somehow `TypeError: list indices must be integers or slices, not float`... So the above is probably just a copy-paste issue... Probably the whole thing is because that code doesn't raise this error, so who knows... – Tomerikoo Nov 19 '20 at 12:05
0

Your data[0] length is 10 and data[1] length is 9 so When you run your tried code you will get Index out of range error so you have to add another if condition that checks if x is not bigger than data[1]'s count before data[1][x] < window_start

data = [[0,1,2,3,4,5,6,7,8,9], [1514764800, 1514851200, 1514937600, 1515024000, 1515110400, 1515196800, 1515283200, 1515369600, 1515456000]]

train_data = [x for x in data[0] if data[0].index(x) < len(data[1]) if data[1][x] < 1514851201 ]
print(train_data)

Output :

[0, 1]
Omer Tekbiyik
  • 4,255
  • 1
  • 15
  • 27