[英]What exactly does this Python code do with my function?
nearest_neighbor
是我稱之為 function 的。 這個 function 返回一個列表...
我無法理解[0] [1]
的含義。這到底是做什么的?
nearest = nearest_neighbor(username,users) [0] [1]
謝謝...
通過使用[0]
和[1]
,您可以從列表中選擇一個元素。 [0]
表示您要訪問列表中的第一個元素,而[1]
表示您要訪問第二個元素。
nearest_neighbor[0] # first element from the output of an function
nearest_neighbor[1] # the second element from the output of an function
您最近的nearest_neighbor()
function 可能會返回列表列表。 使用[0][1]
您可以訪問此列表列表中第一個列表中的第二個元素。
例子:
# This example function returns a list of lists
def nearest_neighbor():
return [[i,i+1] for i in range(3)]
示例 function 的 Output:
>>> nearest_neighbor()
[[0, 1], [1, 2], [2, 3]]
當您像在代碼中一樣調用示例 function 時,它會返回第一個列表的第二個元素,在此示例中為 1:
>>> nearest_neighbor()[0][1]
1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.