Python 字典:将键映射到约束下的不同范围
Python dict: mapping keys to a different range under constraints
我有一本字典 d
其键是 n
整数。键不一定是连续的数字,但是,已知 d
包含 (-num_negative,0)
范围内的 num_negative
个连续键和 (0,num_positive)
范围内的 num_positive
个连续键.我想从它创建另一个字典 d2
,其键是 [0, n]
范围内的连续整数,给定以下约束:
(1) 在 d
中具有来自组 num_positive
的键的元素将在 d2
.
中具有相同的键
(2) d
组 num_negative
中具有负键 i
的元素将在 d2
中具有键 n+i
.
一个例子:
# num_positive = 2, num_negative = 3, n=8
# num_positive group = {0,1}
# num_negative gorup = {-3,-2,-1}
d = {-6: 'h', -3: 'a', -2: 'b', -1: 'c', 0: 'd', 1: 'e', 68: 'f', 99: 'g'}
d2 = {0: 'd', 1: 'e', 2: 'g', 3: 'h', 4: 'f', 5: 'a', 6: 'b', 7: 'c'}
下面的代码应该可以做到。
d1={}
for key,val in d.items():
if key in pos and key<=n : #the elements that had a key from the group num_positive in d, will have the same key in d2
d1[key] = val
elif key in neg and ((key+n) in range(n+1)) : #the elements that had a negative key i from the group num_negative in d, will have the key n+i in d2
d1[key+n] = val
#驱动程序值:
IN : n = 8
IN : pos = [0,1]
IN : neg = [-3,-2,-1]
IN : d = {-6: 'h', -3: 'a', -2: 'b', -1: 'c', 0: 'd', 1: 'e', 68: 'f', 99: 'g'}
OUT : d1 = {5: 'a', 6: 'b', 7: 'c', 0: 'd', 1: 'e'}
注意:OP 的输出存在一些差异。其中很少有与定义不同的行为。
因此,不遵循给定约束的键将被忽略。
如果我理解正确,键在任一连续范围之外的项目在生成的字典中具有未定义的位置。因此,在我的解决方案中,它们使用连续范围 [0, len(d))
中仍然空闲的任何键以任意顺序插入到生成的字典中。详情见评论。
from collections import OrderedDict
d = {-6: 'h', -3: 'a', -2: 'b', -1: 'c', 0: 'd', 1: 'e', 68: 'f', 99: 'g'}
remainder = {}
rPos = range(0, 2 + 1) # Positive consecutive range.
rNeg = range(-3, 0) # Negative consecutive range.
n = len(d)
d2 = OrderedDict([(i, None) for i in range(0, n)])
for k, v in d.items():
if k in rNeg: # Checks if in negative consecutive range.
d2[n + k] = v
elif k in rPos: # Checks if in positive negative range.
d2[k] = v
else: # Key is outside of either range.
remainder[k] = v
for k, v in d2.items():
if v is None: # Finds a key still available for use.
# Pops an arbitrary element from the remainder and inserts its
# value into the new dict using an available key.
d2[k] = remainder.popitem()[1]
Input: {-6: 'h', -3: 'a', -2: 'b', -1: 'c', 0: 'd', 1: 'e', 68: 'f', 99: 'g'}
Output: OrderedDict([(0, 'd'), (1, 'e'), (2, 'g'), (3, 'f'), (4, 'h'), (5, 'a'), (6, 'b'), (7, 'c')])
我有一本字典 d
其键是 n
整数。键不一定是连续的数字,但是,已知 d
包含 (-num_negative,0)
范围内的 num_negative
个连续键和 (0,num_positive)
范围内的 num_positive
个连续键.我想从它创建另一个字典 d2
,其键是 [0, n]
范围内的连续整数,给定以下约束:
(1) 在 d
中具有来自组 num_positive
的键的元素将在 d2
.
(2) d
组 num_negative
中具有负键 i
的元素将在 d2
中具有键 n+i
.
一个例子:
# num_positive = 2, num_negative = 3, n=8
# num_positive group = {0,1}
# num_negative gorup = {-3,-2,-1}
d = {-6: 'h', -3: 'a', -2: 'b', -1: 'c', 0: 'd', 1: 'e', 68: 'f', 99: 'g'}
d2 = {0: 'd', 1: 'e', 2: 'g', 3: 'h', 4: 'f', 5: 'a', 6: 'b', 7: 'c'}
下面的代码应该可以做到。
d1={}
for key,val in d.items():
if key in pos and key<=n : #the elements that had a key from the group num_positive in d, will have the same key in d2
d1[key] = val
elif key in neg and ((key+n) in range(n+1)) : #the elements that had a negative key i from the group num_negative in d, will have the key n+i in d2
d1[key+n] = val
#驱动程序值:
IN : n = 8
IN : pos = [0,1]
IN : neg = [-3,-2,-1]
IN : d = {-6: 'h', -3: 'a', -2: 'b', -1: 'c', 0: 'd', 1: 'e', 68: 'f', 99: 'g'}
OUT : d1 = {5: 'a', 6: 'b', 7: 'c', 0: 'd', 1: 'e'}
注意:OP 的输出存在一些差异。其中很少有与定义不同的行为。
因此,不遵循给定约束的键将被忽略。
如果我理解正确,键在任一连续范围之外的项目在生成的字典中具有未定义的位置。因此,在我的解决方案中,它们使用连续范围 [0, len(d))
中仍然空闲的任何键以任意顺序插入到生成的字典中。详情见评论。
from collections import OrderedDict
d = {-6: 'h', -3: 'a', -2: 'b', -1: 'c', 0: 'd', 1: 'e', 68: 'f', 99: 'g'}
remainder = {}
rPos = range(0, 2 + 1) # Positive consecutive range.
rNeg = range(-3, 0) # Negative consecutive range.
n = len(d)
d2 = OrderedDict([(i, None) for i in range(0, n)])
for k, v in d.items():
if k in rNeg: # Checks if in negative consecutive range.
d2[n + k] = v
elif k in rPos: # Checks if in positive negative range.
d2[k] = v
else: # Key is outside of either range.
remainder[k] = v
for k, v in d2.items():
if v is None: # Finds a key still available for use.
# Pops an arbitrary element from the remainder and inserts its
# value into the new dict using an available key.
d2[k] = remainder.popitem()[1]
Input: {-6: 'h', -3: 'a', -2: 'b', -1: 'c', 0: 'd', 1: 'e', 68: 'f', 99: 'g'}
Output: OrderedDict([(0, 'd'), (1, 'e'), (2, 'g'), (3, 'f'), (4, 'h'), (5, 'a'), (6, 'b'), (7, 'c')])