我試圖找出如何讓衝突查找衝突並設置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這是否回答您的問題? –