簡體   English   中英

3D列表中的Python索引選擇

[英]Python index selection in a 3D list

我有以下 3D 列表:

test = [[[(x,y,z) for x in range(0,5)] for y in range(5,8)] for z in range(0,4)]
test[0].append([(0,5),(5,0)])

我想要 select 第一維的所有索引,第二維的第 0 個索引和第三維的所有索引。 如果它是一個數組,我會寫array[:,0,:] 但是,當我編寫test[:][0][:]時,它與執行test[0][:][:]相同,這不是我想要的。

我怎么能那樣做?

轉置並取第零項。

>>> list(zip(*test))[0]
([(0, 5, 0), (1, 5, 0), (2, 5, 0), (3, 5, 0), (4, 5, 0)],
 [(0, 5, 1), (1, 5, 1), (2, 5, 1), (3, 5, 1), (4, 5, 1)],
 [(0, 5, 2), (1, 5, 2), (2, 5, 2), (3, 5, 2), (4, 5, 2)],
 [(0, 5, 3), (1, 5, 3), (2, 5, 3), (3, 5, 3), (4, 5, 3)])

[thing[0] for thing in test]

三維中的第零項。

 [a[0] for b in test for a in b]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM