[备忘录]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