简体   繁体   English

Python:如何为一个键存储多个值

[英]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我是 Python 新手……我有一组查找键,如果我的值与一组中的任何查找键匹配,那么它应该返回特定值

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有关更多信息,请查看: 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.您可以通过许多不同的方式做您想做的事,一种可能性是将数据和逻辑封装在一个小的 class 中,您在实例化 class 时指定数据,并在调用 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.显示使用比解释它更容易......但是,用于实例化 class 的参数是所需的 arguments 对,每对包含一个包含键的迭代(如果它是单个键,您仍然必须使用一个可迭代的,例如[1] )和与键关联的值。

Here we have the mappings (1,2,3,'a') → 'a' and (4,5,6,'b') → 'b'这里我们有映射(1,2,3,'a') → 'a'(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.我觉得可以为 class 及其实例选择更好的名称……另请注意,我们不检查重复键,最近的 Python 版本按插入顺序扫描键,因此结果(至少)是可预测的。


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如果您愿意,您可以轻松地添加一个默认值,以便在您传递给 object 的密钥不在密钥集中时返回

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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