简体   繁体   中英

How can I remove values less than a certain number from a list of arrays?

I have a dataset that is a list of arrays. Each array is a 30s trial, and within each array is a list of times of events that occurred in the trial over 30s. So for example, one array would be [0.2, 3., 5., 6.2,.....29.99].

I want to plot only the events that occurred in the first 10s of each trial using eventplot. This is what I have tried:

plt.eventplot(test_spikes[test_spikes<10]);

But I get the error "'<' not supported between instances of 'list' and 'int'"

I'm not sure what I'm doing wrong. Thank you for your help in advance!

Try

[d for d in data if d < 10]

You have first to convert to numpy array:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.uniform(low=0.5, high=20,size=(50,))
# data = np.array(data) # not necessary as it is a numpy array
print(data.shape)   # (50,)
plt.eventplot(data[data<10])

Output: 在此处输入图像描述

As you have a list of (what I assume are) numpy arrays, you need to perform your comparison for each array individually. The error message is trying to tell you this; the outer list is not a numpy array and therefore does not support comparison with an integer.

to_plot = [x[x<10] for x in test_spikes]

For this to work, x must be np.array s. If that is not given, you can convert them as such:

test_spikes = [np.array(x) for x in test_spikes]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM