Python - Use a variable as a list name

后端 未结 3 1708
故里飘歌
故里飘歌 2020-12-07 03:19

I have this code in python.

import sys
list1 = [\"A\", \"B\", \"C\"]
list2 = [1, 2, 3]

myarg = sys.argv[1]
print len(myarg)

I will run thi

3条回答
  •  自闭症患者
    2020-12-07 04:03

    You shouldn't, but if you really want/have to do this, you can use globals():

    print len(globals()[myarg])
    

    First make sure myarg is the name of a declared variable: if myarg in globals(): ...

提交回复
热议问题