Python,如何传递对列表列表的引用(不是值)

Python,如何传递对列表列表的引用(不是值),python,list,pass-by-reference,Python,List,Pass By Reference,我希望传递对int列表的引用,而不是int值本身 我有一个整数列表,它代表一个根第一二叉树,我调用它 当我沿着aTriangle 路径中充满了对aTriangle格式的aTriangle[i][j] 问题是,当我试图打印或传递路径时,它是一个int值列表,而不是我放入其中的引用列表 比如说, test_triangle = [[1], [2, 3], [4, 5, 6]] test_list = [test_triangle[0][0], test_triangle[1][0]] print(t

我希望传递对int列表的引用,而不是int值本身

我有一个整数列表,它代表一个根第一二叉树,我调用它 当我沿着
aTriangle
路径
中充满了对
aTriangle
格式的
aTriangle[i][j]
问题是,当我试图打印或传递
路径时,它是一个int值列表,而不是我放入其中的引用列表

比如说,

test_triangle = [[1], [2, 3], [4, 5, 6]]
test_list = [test_triangle[0][0], test_triangle[1][0]]
print(test_list)
打印出
[1,2]
(同样,通过测试\u列表也会通过值)

但是我想要
[测试三角形[0][0],测试三角形[1][0]


如何建立作为引用保留的引用列表?或者,如果这是不可行的,如果我有另一种方法来保持这两个索引与该值关联,因为这些索引值对后面的步骤很重要。

您正在寻找类似的方法吗

免责声明->这是纯粹的黑客行为,我建议寻找更复杂的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)
输出

{'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}

您似乎误解了python列表的工作原理,当您为列表列表编制索引时,您得到的实际上是对所包含列表的引用。。但是,当您打印它时,python足够聪明,可以打印引用列表的repr,而不是列表的地址,就像在c/c++中一样。是否有另一种方法可以让两个索引的tract与值yes关联?将索引存储在
test\u list
中。“是否有另一种方法可以让两个索引的tract与值关联”。。查找xarray,xarray中包含的元素存储它们所包含的索引。Python始终具有引用语义(尽管它既不是按值调用也不是按引用调用)@倒计时积分你知道吗,这是完美的。这是一种简单、直接的方法来保存我的索引,当我最终需要这个值时,我可以很容易地调用它。谢谢!这很好,谢谢