简体   繁体   中英

Removing elements from list based on index from another list

I have sample of my code,where I am trying to filter some data.

import numpy as np
from scipy.signal import argrelextrema

data = np.arange(data)
index = argrelextrema(data,np.greater_equal)[0]
value = [data[i] for i in index if data[i] > 1000] 

My problem is that after
if data[i] > 1000 value list has less elements than index list.So the elements I have removed from value list i also want to remove from index list.But index list contains only (surprise) index of values from data list.

I'd be tempted to create a new filtered index list to go alongside your new value list.

value = [data[i] for i in index if data[i] > 1000] 
findex = [i for i in index if data[i] > 1000] 

Now findex contains a 1-1 set of filtered indexes that you can use alongside your value list. It might be better to unpack both variables at the same time, and then using zip to pull them apart using something like:

mix = [(i,data[i]) for i in index if data[i] > 1000] 
xim = [a for a in zip(*mix)]
indexes = list(xim[0])
values = list(xim[1])

Again, I'm sure there's a more elegant way, but here you can see what's happening at each stage.

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