Python argparse: makes list item unique

The ability to check list items with choices=servers below is good.

 servers = [ "ApaServer", "BananServer", "GulServer", "SolServer", "RymdServer", "SkeppServer", "HavsServer", "SovServer" ] parser = argparse.ArgumentParser() parser.add_argument('-o', '--only', nargs='*', choices=servers, help='Space separated list of case sensitive server names to process') 

Is it possible to make an item in a list be unique so duplicates are not allowed?

+2
source share
4 answers

By properly deleting duplicates with argparse , your own argparse.Action class will be created that takes care of using set , as suggested by other answers:

 import argparse class UniqueAppendAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): unique_values = set(values) setattr(namespace, self.dest, unique_values) servers = ["ApaServer", "BananServer", "GulServer", "SolServer", "RymdServer", "SkeppServer", "HavsServer", "SovServer" ] parser = argparse.ArgumentParser() parser.add_argument('-o', '--only', nargs='*', choices=servers, action=UniqueAppendAction, help='Space separated list of case sensitive server names to process') print parser.parse_args() 

Output Example:

 $ python test.py -o ApaServer ApaServer Namespace(only=set(['ApaServer'])) 
+3
source

I don't think you can provide this with argparse , but I also see no reason for this. Just write to help that duplicates are ignored. If the user passes duplicate arguments to --only , just let her do this and ignore the duplicate argument when processing the argument arguments (for example, by iterating over the list in set() before processing).

+3
source

Michelle's answer change:

 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] 

Significantly shorter.

+2
source

Here is an excerpt from some code that I use for a similar purpose:

 def parse_args(argv): class SetAction(argparse.Action): """argparse.Action subclass to store distinct values""" def __call__(self, parser, namespace, values, option_string=None): try: getattr(namespace,self.dest).update( values ) except AttributeError: setattr(namespace,self.dest,set(values)) ap = argparse.ArgumentParser( description = __doc__, formatter_class = argparse.ArgumentDefaultsHelpFormatter, ) ap.add_argument('--genes', '-g', type = lambda v: v.split(','), action = SetAction, help = 'select by specified gene') 

This is similar in spirit to the jcollado answer with a slight improvement: you can specify several options with values โ€‹โ€‹separated by commas, and they are released (via a set) in all respects.

For instance:

 snafu$ ./bin/ucsc-bed -g BRCA1,BRCA2,DMD,TNFA,BRCA2 -g BRCA1 Namespace(genes=set([u'BRCA1', u'BRCA2', u'DMD', u'TNFA'])) 

Note that there are two -g arguments. BRCA2 is indicated twice in the first, but appears only once. BRCA1 is indicated in the first and second variants of -g, but also appears only once.

+2
source

Source: https://habr.com/ru/post/908971/


All Articles