可以说我有
lags = [0, 30, 60, 90, 120, 150, 180, np.inf]
和
list = [[500, 800, 1000, 200, 1500], [220, 450, 350, 1070, 1780], [900, 450, 1780, 1450, 100], [340, 670, 830, 1370, 1420], [850, 630, 1230, 1670, 910]] angle = [[50, 80, 100, 20, 150], [22, 45, 35, 107, 178], [90, 45, 178, 145, 10], [34, 67, 83, 137, 142], [85, 63, 123, 167, 91]]
我想将每个元素放在列表中,并根据其值将其存储在不同的单独数组中;
for all list.values where angles.value is less than 30 list1 = [200, 220, 100] for all list.values where angles.value is between 30 and 60 list2 = [500, 450, 350, 450, 340] for all list.values where angles.value is between 60 and 90 list3 = [800, 670, 830, 850, 630]
等等..
我做了这样的事情:
sortlist = defaultdict(list) ulist = np.unique(list) uangle = np.unique(angle) for lag in lags: count += 1 for k, dummy_val in enumerate(uangle): if lag <= uangle[k] < lag + 1: sortlist[count].append(ulist[k])
我想知道是否有一种pythonic /有效的方法来提高性能.
这是一个矢量化的方法 -
an = angle.ravel() sidx = an.argsort() cut_idx = np.searchsorted(an[sidx], lags) out = np.split(list1.ravel()[sidx], cut_idx[1:-1])
样本输入,输出 -
In [97]: lags = np.array([0, 30, 60, 90, 120, 150, 180, np.inf]) ...: ...: list1 = np.array([[500, 800, 1000, 200, 1500], \ ...: [220, 450, 350, 1070, 1780], \ ...: [900, 450, 1780, 1450, 100], ...: [340, 670, 830, 1370, 1420], \ ...: [850, 630, 1230, 1670, 910]]) ...: ...: angle = np.array([[50, 80, 100, 20, 150],\ ...: [22, 45, 35, 107, 178],\ ...: [90, 45, 178, 145, 10], ...: [34, 67, 83, 137, 142],\ ...: [85, 63, 123, 167, 91]]) ...: In [99]: out Out[99]: [array([100, 200, 220]), # <----- 0 to 30 array([340, 350, 450, 450, 500]), # <----- 30 to 60 array([630, 670, 800, 830, 850]), # <----- 60 to 90 array([ 900, 910, 1000, 1070]), # <----- 90 to 120 array([1230, 1370, 1420, 1450]), # <----- 120 to 150 array([1500, 1670, 1780, 1780]), # <----- 150 to 180 array([], dtype=int64)] # <----- 180 to Inf