我试图找出如何让冲突查找冲突并设置Python的
nbr_dict = {'key1': {1, 2, 3}, 'key2': {4, 5, 6}, 'key3': {1,5}}
我希望它看起来像
Clashes?: key1
key1 and key2: None
key1 and key3: 1
Clashes?: key2
key2 and key1: None
key2 and key3: 5
Clashes?: key3
key3 and key1: 1
key3 and key2: 5
这里是我的代码的例子,我设法得到:
def main():
nbr_dict = {'key1': {1, 2, 3}, 'key2': {4, 5, 6}, 'key3': {1,5}}
clashes = input('Clashes?: ')
for key in sorted(nbr_dict):
if key != clashes:
print('{} and {}'.format(clashes, key))
#I'm pretty sure all the arithmetic goes here#
main()
假定用户给出的所有输入都是有效的
1.整理字典有什么意义? 2.你将'key'与'clashes'进行比较,但是'key'在该上下文中是'int',而'clash'是key1/key2/key3 - 他们为什么会*相等? – alfasin
回答你的问题,1:对它进行排序,如果我说我添加了一个新的键值'key0':{5,6}输出将是:冲突?:key1 key1和key0:无 key1和key2:无 key1和key3:1 2.该键实际上是一个字符串,'key1','key2'等,还是你实际上是指这些值?不等于检查密钥是否与冲突相同,以便它不会输出密钥1和密钥1或密钥2和密钥2或密钥3和密钥3这是否回答您的问题? –