亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

追加到一個numpy數組

追加到一個numpy數組

精慕HU 2021-05-13 18:14:48
我有以下代碼:result = np.zeros((samples,), dtype=[('time', '<f8'), ('data', '<f8', (datalen,))])我想創建tempresult一個累積數據的變量,result一旦累積了25000個樣本,我想對其執行一些操作。所以我想做些類似的事情:result = np.zeros((samples,), dtype=[('time', '<f8'), ('data', '<f8', (datalen,))])tempresult.append(result)if ( len(tempresult[0]  > 25000 )):   # do something我嘗試了答案代碼,但出現異常TypeError:無效的類型提升result = np.zeros((samples,), dtype=[('time', '<f8'), ('data', '<f8', (datalen,))])        if self.storeTimeStamp:            self.storeTimeStamp = False            self.timestamp = message.timestamp64            self.oldsamples = 0        for sample in range(0, samples):            sstep = self.timestamp + (self.oldsamples + sample) * step            result[sample] = (sstep, data[sample])        self.oldsamples = self.oldsamples + samples        # append        np.append(self.tempresult, result)        if len(self.tempresult) < 25000:            return        return [self.tempresult]
查看完整描述

1 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

1)閱讀np.append文檔。

np.append(self.tempresult, result)

是錯的。 np.append返回一個新數組;它的作用不像列表追加那樣。

2)np.append是的笨拙界面np.concatenate。如果你聽不懂concatenate,就會被搞砸了append。

3)因為每次都會創建一個新數組,所以重復的連接速度很慢。收集數組列表并在最后進行一個連接要快得多

4)使用化合物時dtype,所有輸入concatenate必須具有相同的dtype。


查看完整回答
反對 回復 2021-05-18
  • 1 回答
  • 0 關注
  • 217 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號