處理優化劃分2維列表 a = [[0,2195,554],[1878,0454]] 得到一個整數結果: 現在我有一個瘋狂低效的for循環 - 我看過一些關於映射爲int,並使用列表理解,但掙扎的文檔瞭解如何使之成爲一個二維表python快速劃分多維列表的方法
for row in range(0, len(a)):
for col in range(0, len(a[row])):
a[row][col] = int(a[row][col]/600)
處理優化劃分2維列表 a = [[0,2195,554],[1878,0454]] 得到一個整數結果: 現在我有一個瘋狂低效的for循環 - 我看過一些關於映射爲int,並使用列表理解,但掙扎的文檔瞭解如何使之成爲一個二維表python快速劃分多維列表的方法
for row in range(0, len(a)):
for col in range(0, len(a[row])):
a[row][col] = int(a[row][col]/600)
你的工作不需要範圍循環,你可以使用列表組合:
a[:] = [[ele // 600 for ele in sub] for sub in a]
a[:]
將改變原來的列表,就像你的代碼正在做一個列表組件的效率一樣。如果你想要進一步的顯着改善,你應該看看numpy。
import numpy as np
a = np.array(a)
a //= 600
print(a)
[[0 3 9]
[3 0 0]]
謝謝!解決了我一直在尋找的問題 – user3068287
不用擔心,不客氣。 –
獲得重大改進的唯一方法是用numpy – inspectorG4dget