Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

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

I want to pass a refference to a list of lists of ints, NOT the int value itself.

I have a list of list of ints, which represents a root-first binary tree, which I am calling aTriangle I am building a list called path as I am walking down aTriangle path is being filled with references to aTriangle of the format aTriangle[i][j] the problem is that when I try to print or pass path, it is a list of int values, NOT a list of the references that I put in it.

For example,

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

prints out [1, 2] (similarly, passing test_list passes on the values)

but I want [test_triangle[0][0], test_triangle[1][0]]

How do I build a list of references that STAY as references? Or if this is infeasible, if there another method for me to keep tract of both indexes associated with the value, since these index values are important to later steps.

question from:https://stackoverflow.com/questions/65850620/python-how-to-pass-reference-to-list-of-list-not-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Are you looking for something like this?

Disclaimer -> This is pure hack, I would suggest to look for more sophisticated python module/library.

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}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...