lags = [0,30,60,90,120,150,180,np.inf]
和
List = [[500,800,1000,200,1500],[220,450,350,1070,1780],[900,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,178,145,10],[34,67,83,137,142],[85,63,123,167,91]]
我想将每个元素放在列表中,并根据其值将其存储在不同的单独数组中;
for all List.values where angles.value is less than 30List1 = [200,220,100]for all List.values where angles.value is between 30 and 60List2 = [500,340] for all List.values where angles.value is between 60 and 90List3 = [800,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,np.inf]) ...: ...: List1 = np.array([[500,\ ...: [220,\ ...: [900,...: [340,\ ...: [850,910]]) ...: ...: angle = np.array([[50,\ ...: [22,\ ...: [90,...: [34,\ ...: [85,91]]) ...: In [99]: outOut[99]: [array([100,220]),# <----- 0 to 30 array([340,500]),# <----- 30 to 60 array([630,850]),# <----- 60 to 90 array([ 900,910,1070]),# <----- 90 to 120 array([1230,1420,1450]),# <----- 120 to 150 array([1500,1780]),# <----- 150 to 180 array([],dtype=int64)] # <----- 180 to Inf总结
以上是内存溢出为你收集整理的python – 有效地按元素分组全部内容,希望文章能够帮你解决python – 有效地按元素分组所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)