Your example creates multiple key: value pairs if using fromkeys. If you don't want this, you can use one key and create an alias for the key. For example if you are using a register map, your key can be the register address and the alias can be register name. That way you can perform read/write operations on the correct register.
However, if you have a static dictionary, and you need only access values by multiple keys then you could just go the very simple route of using two dictionaries. One to store the alias key association and one to store your actual data:
def add(key, id, value=None)
if id in dictionary:
if key in alias:
# Do nothing
pass
else:
alias[key] = id
else:
dictionary[id] = value
alias[key] = id
add('e', 'id2')
add('f', 'id3', 3)
In [6]: a = 10
In [7]: id(a)
Out[7]: 10914656
In [8]: b = 10
In [9]: id(b)
Out[9]: 10914656
In [10]: c = 11
In [11]: id(c)
Out[11]: 10914688
In [12]: d = 21
In [13]: id(d)
Out[13]: 10915008
In [14]: e = 11
In [15]: id(e)
Out[15]: 10914688
In [16]: e = 21
In [17]: id(e)
Out[17]: 10915008
In [18]: e is d
Out[18]: True
In [19]: e = 30
In [20]: id(e)
Out[20]: 10915296
在上面的输出中,变量 a 和 b 共享相同的内存,当我创建一个新的变量 e 并存储一个已经存在于变量 c 中的值(11)时,c 和 d 有不同的内存,所以它映射到那个内存位置,当我将变量 e 中的值改为21时,它不会创建一个新的内存,这个值已经存在于变量 d 中,所以现在变量 d 和 e 共享相同的内存位置。最后,我将变量 e 中的值改为30,这个值不存储在任何其他变量中,因此它为 e 创建了一个新的内存。
所以任何具有相同值的变量都共享内存。
不适用于列表和字典对象
我们来谈谈你的问题。
当多个键具有相同的值时,则所有键共享相同的内存,因此您所期望的内容已经存在于 python 中。
你可以像这样简单地使用它
In [49]: dictionary = {
...: 'k1':1,
...: 'k2':1,
...: 'k3':2,
...: 'k4':2}
...:
...:
In [50]: id(dictionary['k1'])
Out[50]: 10914368
In [51]: id(dictionary['k2'])
Out[51]: 10914368
In [52]: id(dictionary['k3'])
Out[52]: 10914400
In [53]: id(dictionary['k4'])
Out[53]: 10914400
从上面的输出,键 k1和 k2映射到相同的地址,这意味着值1存储在内存中只有一次,这是多键单值字典这是你想要的东西。校对: P
#!/usr/bin/env python3
def get_keys(s):
# Lower the user's entry to easily manipulate data
s = s.lower()
# Create a list to simulate multiple keys
numbers = ['uno', 'one', 'um', 'eins', 'ein']
# Lambda for input validation
validator = lambda key: key if key in numbers else 'no-key-found' # return me x if x is found in the list numbers, contratiwise return me 'no-key-found'
dic = {
validator(s):'1',
'no-key-found':'Key does not exist'
}
return dic[validator(s)]
print(get_keys(input('Type in word: ')))
有动态钥匙
#!/usr/bin/env python3
import sys
def week_days():
# Assets
number_day = ['1', '2', '3', '4', '5', '6', '7']
# Introduction
print('Welcome to the Week Day Finder')
# User input
day = input('Please, enter the day you want to check: ').lower()
WEEK_COLOR = {'RED': '\u001b[31m', 'GREEN': '\u001b[32m'}
# Day validator
days = {
'1' if day in number_day else 'sunday': 'Weekend Day',
'2' if day in number_day else 'monday': 'Week Day',
'3' if day in number_day else 'tuesday': 'Week Day',
'4' if day in number_day else 'wednesday': 'Week Day',
'5' if day in number_day else 'thursday': 'Week Day',
'6' if day in number_day else 'friday': 'Week Day',
'7' if day in number_day else 'saturday': 'Weekend Day'
}
# Logical trial
try:
if days[day] == 'Week Day':
print(WEEK_COLOR['GREEN'], days[day])
else:
print(WEEK_COLOR['RED'], days[day])
except KeyError:
print('** Invalid Day **', file=sys.stderr)
def main():
week_days()
if __name__ == '__main__':
main()