編寫一個函數或程序,它將採用2個整數「當前」和「目標」數組,並生成2個數組,表示添加列表和刪除列表,以便將添加和刪除應用於「當前「數組將產生」目標「數組。Python Array Diff
例如,給定以下 輸入:
current = [1, 3, 5, 6, 8, 9]
target = [1, 2, 5, 7, 9]
的輸出將是:
additions: [2, 7]
deletions: [3, 6, 8]
所以,以下爲真:
電流([1, (3,5,7,9)] +添加物([2,7]) - 刪除物([3,6,8])=目標物([1,2,5,7,9])
解決方案:
到目前爲止,我有這樣的:
---------------------------
# import array function
from array import array
# create an integer array named current
current = array('i', [1, 3, 5, 6, 8, 9])
# add items from additions list into current array using the fromlist() method
additions = [2, 7]
current.fromlist(additions)
# remove items on deletions list from current array using the. remove() method
current.remove(3)
current.remove(6)
current.remove(8)
什麼問題? – Dekel
你能澄清「不起作用」嗎?你有錯誤嗎? – Dekel
道歉 - 它或多或少的作品,但是,當我一步一步地進入最終名單時, –