简体   繁体   中英

Python: Using a returned value to reference another variable?

I have a program that calls a function, and the function returns an integer within a range of 1-25. I have a corresponding 25 lists in my program that, based on the value of this returned integer, I then need to pass to another function.

For example:

List1 = [1, 45]
...
List25 = [4, 584]

def ReturnListRef():
    a = 24
    return a

What would be the quickest way to use the returned value of '24' to send List24 to my next function?

Seems like it should be so easy to do, but after finishing up my beginners guide to Python book, I still can't achieve this without what seems like too many steps!

Use a dict to store your lists:

d = {24:[1, 45],25:[4, 584]}

def ReturnListRef():
    a = 24
    return a


def foo():
    var = d[ReturnListRef()]
    print var
print  foo()
[1, 45]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM