考虑一下,我有两组字符串元素(不必要的字符串-仅用于示例):
“ 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'}
在这种情况下,我可以在两个列表中按键找到一个元素,但是我必须为此保留两个映射。
方法3: 创建有意义的索引(手动或使用枚举)并使用特殊参数从元组对中选择一个元素:
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
还有其他方法吗?
答案 0 :(得分:1)
您也可以尝试使用bidict
库:
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
答案 1 :(得分:1)
正向和反向映射可能是您的最佳选择。您可以通过一种方法(通过添加反向字典的字典子类)访问单个对象。
显然,尽管两个字典将使用对其中包含的相同对象的引用,而不是重复的对象,但是仍然需要分配内存。
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
这需要