簡體   English   中英

Python,如何傳遞對列表列表的引用(不是值)

[英]Python, how to pass reference to list of list (not value)

我想傳遞對 int 列表列表的引用,而不是 int 值本身。

我有一個整數列表的列表,它代表一個根優先的二叉樹,我稱之為aTriangle我正在構建一個名為path的列表,因為我正在沿着aTriangle path行走,其中填充了對aTriangle格式的引用aTriangle[i][j]問題是當我嘗試打印或傳遞path時,它是一個 int 值列表,而不是我放入其中的引用列表。

例如,

test_triangle = [[1], [2, 3], [4, 5, 6]]
test_list = [test_triangle[0][0], test_triangle[1][0]]
print(test_list)

打印出[1, 2] (類似地,通過 test_list 傳遞值)

但我想要[test_triangle[0][0], test_triangle[1][0]]

如何建立一個作為參考的參考列表? 或者,如果這是不可行的,如果我有另一種方法來記錄與該值相關的兩個索引,因為這些索引值對后面的步驟很重要。

你在尋找這樣的東西嗎?

免責聲明-> 這純粹是 hack,我建議尋找更復雜的 python 模塊/庫。

test_triangle = [[1], [2, 3], [4, 5, 6]]
test_list = (test_triangle[0][0], test_triangle[1][0])

values_and_indexes = {}

for index, value in enumerate(test_triangle):
    for _i, _v in enumerate(value):
        values_and_indexes[f"test_triangle[{index}][{_i}]"] = _v

print(values_and_indexes)

OUTPUT

{'test_triangle[0][0]': 1, 'test_triangle[1][0]': 2, 'test_triangle[1][1]': 3, 'test_triangle[2][0]': 4, 'test_triangle[2][1]': 5, 'test_triangle[2][2]': 6}

暫無
暫無

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

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