Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中创建两个组元素之间的映射?_Python_Python 3.x_Mapping - Fatal编程技术网

如何在python中创建两个组元素之间的映射?

如何在python中创建两个组元素之间的映射?,python,python-3.x,mapping,Python,Python 3.x,Mapping,假设我有两组字符串元素(不必要的字符串-仅用于示例): “strA1、strA2、strA3”和“strB1、strB2、strB3”。 我想将第一个列表中的元素与第二个列表中的元素映射成唯一的对,根据已知的规律,我可以在第二个列表中按第一个列表中的元素找到元素,反之亦然。 我知道三种方法 方法1:创建地图 {'strA1':'strB1', 'strA2':'strB2', 'strA3':'strB3'} 在本例中,我可以通过键从第二个列表中找到一个元素。但如果我想从第一个列表中找到元素,

假设我有两组字符串元素(不必要的字符串-仅用于示例): “strA1、strA2、strA3”和“strB1、strB2、strB3”。 我想将第一个列表中的元素与第二个列表中的元素映射成唯一的对,根据已知的规律,我可以在第二个列表中按第一个列表中的元素找到元素,反之亦然。
我知道三种方法

方法1:创建地图

{'strA1':'strB1', 'strA2':'strB2', 'strA3':'strB3'}
在本例中,我可以通过键从第二个列表中找到一个元素。但如果我想从第一个列表中找到元素,我必须遍历所有字典键

方法2:创建两个映射:

{'strA1':'strB1', 'strA2':'strB2', 'strA3':'strB3'}

{'strB1':'strA1', 'strB2':'strA2', 'strB3':'strA3'}
from enum import Enum
Index = Enum('Index', ['PAIR_A1B1', 'PAIR_A2B2', 'PAIR_A3B3'], start=0)  #understandable names
 #direction
A2B = 0
B2A = 1   
mappingList = [('strA1','strB1'), ('strA2','strB2'), ('strA3','strB3')]
print(mappingList[Index.PAIR_A1B1.value][A2B]) # I get my mapped string here
在这种情况下,我可以在两个列表中按键找到一个元素,但我必须为此保留两个映射

方法3:创建有意义的索引(手动或使用enum),并使用特殊参数从元组中的一对中选择一个元素:

{'strA1':'strB1', 'strA2':'strB2', 'strA3':'strB3'}

{'strB1':'strA1', 'strB2':'strA2', 'strB3':'strA3'}
from enum import Enum
Index = Enum('Index', ['PAIR_A1B1', 'PAIR_A2B2', 'PAIR_A3B3'], start=0)  #understandable names
 #direction
A2B = 0
B2A = 1   
mappingList = [('strA1','strB1'), ('strA2','strB2'), ('strA3','strB3')]
print(mappingList[Index.PAIR_A1B1.value][A2B]) # I get my mapped string here
还有其他方法吗?

您也可以尝试使用library:

from bidict import bidict

group1=["strA1", "strA2", "strA3"]   
group2=["strB1", "strB2", "strB3"]
    
dc=dict(zip(group1,group2))   #we create the dictionary by using zip to create the tuples and then cast it to a dict
    
newdc= bidict(dc)             #we create the bi-directional dictionary

print(newdc['strA1'])
print(newdc.inverse['strB1'])
输出:

strB1
strA1

正向和反向映射可能是您的最佳选择。下面是一种通过单个对象(添加了反向字典的dictionary子类)访问它们的方法

显然,它仍然需要分配内存,尽管这两个字典将使用对其中包含的相同对象的引用,而不是重复的对象

class Map(dict):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._reversed = dict((v, k) for k, v in self.items())
        if len(self) != len(self._reversed):
            raise ValueError("values not unique")

    def __setitem__(self, k, v):
        if v in self._reversed:
            raise ValueError("attempted item assignment would create many-to-one mapping")        
        if k in self:
            # reassigning - need to delete reversed key first
            del self._reversed[self[k]]  # or just "del self[k]" here
        super().__setitem__(k, v)
        self._reversed[v] = k

    def __delitem__(self, k):
        del self._reversed[self[k]]
        super().__delitem__(k)

    def get_reversed(self, v):
        return self._reversed[v]


if __name__ == '__main__':

    a = Map({'strA1':'strB1', 'strA2':'strB2', 'strA3':'strB3'})

    a["strA4"] = "strB4"
    a["strA5"] = "strB5"

    # in each of forward and reversed direction, test
    # one of the pairs that we started with, and one of 
    # the pairs that we added afterwards

    for k in "strA2", "strA4":
        print(a[k])

    for k in "strB3", "strB5":
        print(a.get_reversed(k))
给出:

strB2
strB4
strA3
strA5

它需要

可能有帮助。@Asocia它有帮助!谢谢!很不错的!功能强大且文档清晰。