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

前端 未结 7 1818
滥情空心
滥情空心 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:49

    Why not:

    python foo.py 1,2,3,4 5,6,7,8  
    

    Much cleaner than trying to eval python and doesn't require your user to know python format.

    import sys
    
    list1 = sys.argv[1].split(',')
    list2 = [int(c) for c in sys.argv[2].split(',')]  # if you want ints
    

提交回复
热议问题