![](/img/trans.png)
[英]Numpy: Why advance indexing of > 2 dimensional arrays with a list and numpy array give different results
[英]Why the last value indexing in tuple or list give different class and slicing give different in python?
例如,如果
a=(1,[2,3])
然后索引
b=a[1]
將給出 class列表(因為索引值是列表)
[2, 3] <class 'list'>
但是在切片時
c=a[1:]
給出 class元組
([2, 3],) <class 'tuple'>
誰能解釋為什么會這樣? 在確定 class 方面,Python 如何區分索引和切片?
先感謝您。
索引為您提供序列中的特定項目,而切片返回其中的一個片段,即使該片段是一個元素長。
元組由許多支持索引和切片操作的任意對象/異構項組成。
以下將始終為您提供一個元組:
t = tuple()
t[start:stop]
t[start:]
t[:stop]
t[:]
如果您將使用索引來訪問像t[0]
或t[-1]
這樣的tuple
中的對象,那么不同索引的類型將有所不同,具體取決於該特定索引上存在的內容。
切片返回序列的切片(即相同類型),而索引返回該索引處的項目。 相比:
>>> t = [1, 2, 3]
>>> t[1:] # Slice -> list
[2, 3]
>>> t[1] # Index -> int
2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.