def check():
dict_choice_a = {(a, b) : value, (b, a) : value} #(a, b) and (b, a) refer to the same value but repeted
dict_choice_b = {tuple(sorted((a, b)) : value} #not repetitive but unreadable
dict_choice_a[(a, b)] = new_value #need to do twice to change value but more readable than dict_choice_b
dict_choice_a[(b, a)] = new_value
#value of both keys are always the same
我想创建一个dictionary
,它的元组键引用其值,该键需要作为(a, b) = (b, a)
交换,并且它们只引用相同的值。在
这里的问题是:什么是最好的方法,使郁金香元素的钥匙交换,但也指相同的价值。在
此外,字符串也应该在解决方案中起作用。在
根据注释,可以将}放入一个无序的^{} :
a
和{如果需要自动执行此操作,可以创建自己的^{} :
^{pr2}$使用中:
但是请注意,对于其他类型的键,这可能会有意外的行为:
使用@jornsharpe解决方案,我为其他类型的键的意外行为创建了一个替代方案,考虑到只有元组将以无序的方式使用:
相关问题 更多 >
编程相关推荐