How to pass an entire list as command line argument in Python?

前端 未结 7 1851
滥情空心
滥情空心 2020-12-02 16:59

I was trying to pass two lists containing integers as arguments to a python code. But sys.argv[i] gets the parameters as a list of string.

Input would

7条回答
  •  执念已碎
    2020-12-02 17:55

    I tested this on my end, and my input looks like this:

    python foo.py "[1,2,3,4]" "[5,6,7,8,9]"
    

    I'm doing the following to convert the two params of interest:

    import ast
    import sys
    
    list1 = ast.literal_eval(sys.argv[1])
    list2 = ast.literal_eval(sys.argv[2])
    

提交回复
热议问题