简体   繁体   中英

Python: How to store multiple values for one key

I am new to Python… I have set of lookup keys and if my value matches with any of the lookup key in one set then it should return particular value

Lookup_key_n1 OR Lookup_key_n2 OR Lookup_key_n3 OR Lookup_key_n4 ==> return value1

Lookup_key_m1 OR Lookup_key_m2 OR Lookup_key_m3 OR Lookup_key_m4 ==> return value2

How can I achieve this type of pattern effectively?

Use set data structure. For more information check this: https://www.programiz.com/python-programming/set

You can simply put your values in a set and check if your value is in the set.

If the lookup is supposed to be quick and the values to which the keys point are not particularly memory-consuming, it might be easiest to have a separate entry in a dictionary for every key:

key_val_dict = {'Lookup_key_n1': 'value1', 'Lookup_key_n2': 'value1', 'Lookup_key_n4': 'value1','Lookup_key_m1':'value2', 
'Lookup_key_m2': 'value2', 'Lookup_key_m3': 'value2', 
'Lookup_key_m4': 'value2'}

You can do what you want in many different ways, a possibility is to encapsulate data and logic in a small class, you specify the data when you instantiate the class and use the associated logic when you call the class.

In [72]: class discrete_function(): 
    ...:     def __init__(self, *args): 
    ...:         self.d = {frozenset(k):v for k, v in zip(*[iter(args)]*2)} 
    ...:     def __call__(self, value): 
    ...:         for k in self.d: 
    ...:             if value in k: return self.d[k] 

Showing the use is easier than explaining it... however, the argument used to instantiate the class are as many couples of arguments as required, each couple consisting of an iterable containing the keys (if it's a single key, you have still to use an iterable, eg, [1] ) and the value associated with the keys.

Here we have the mappings (1,2,3,'a') → 'a' and (4,5,6,'b') → 'b'

In [73]: chooser = discrete_function((1,2,3,'a'),'a', (4,5,6,'b'),'b')                    

In [74]: for x in (1,2,3,8,4,5,6,8,'a','b','c'): print(chooser(x))                        
a
a
a
None
b
b
b
None
a
b
None

In [75]:    

I feel one could choose better names for the class and its instance… Note also that we don't check for duplicate keys, recent Python versions scan the keys in order of insertion so the outcome is however (at least) predictable.


Addendum

If you want, you can easily, optionally add a default value to be returned when the key you pass to the object is not in the sets of keys

class discrete_function(): 
    def __init__(self, *args, default=None): 
        self.d = {frozenset(k):v for k, v in zip(*[iter(args)]*2)}
        self.default = default
    def __call__(self, value): 
        for k in self.d: 
            if value in k: return self.d[k]
        return self.default

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM