Python argparse: Force a list item to be unique

后端 未结 4 1705
广开言路
广开言路 2021-01-19 16:17

Being able to validate the list items using choices=servers below is nice.

servers = [ \"ApaServer\", \"BananServer\", \"GulServer\", \"SolServ         


        
4条回答
  •  执笔经年
    2021-01-19 16:46

    Modifying Michel's answer:

    In [1]: x = [5,6,5]
    
    In [2]: x_nodups = list(set(x))
    
    In [3]: x_nodups
    Out[3]: [5, 6]
    
    In [4]: x_nodups_michel = dict(map(lambda i: (i,1),x)).keys()
    
    In [5]: x_nodups_michel
    Out[5]: [5, 6]
    

    Much shorter.

提交回复
热议问题