Is it possible to pass a list variable as an argument to the argparse module?
It has been seen to use nargs
option
parser.add_argument('--my_arg', --nargs='+', type=int)
and pass some values as:
python script.py --my_arg 2 6 9
Instead, is it possible to do the following?
my_list=[2,6,9]
python script.py --my_arg my_list
This depends on the operating system and shell used. For example, if you are on a unix-like system and using zsh, you could do
my_list=(2 6 9)
python script.py --my_arg $my_list
Zsh is quite forgiving here as it will expand entire arrays if you omit the index and, unlike most shells, it won't split elements of my_list
into separate words if there are spaces in them. In Bash, I think you have to do something like this
my_list=(1 2 "hello there")
python script.py --my_arg "${my_list[@]}"
Chech the bash manual on arrays for details.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.