[備忘錄]python argparser傳入列表參數


import argparse
 
parser = argparse.ArgumentParser()

# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')
 
# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

 

輸出

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']
 
$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]
 
$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

轉自https://blog.csdn.net/kinggang2017/article/details/94036386


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM