我有以下的,需要對蟒蛇implementation..I一些指導明確記載的算法和預期output..any輸入真正的讚賞..解析字典鍵和值和寫入/數據附加到文件
data_changes ={'305403': ['302180'], '312994': ['311957'], '311957': ['312621'] }
modem_changes = {'305403': [], '313113': [], '312994': ['253036', '312591'], '311957': []}
for keys that are present in both data_changes and modem_changes:
write data to a file "file.txt" in the order key-->data_changes_values-->modem_changes_values
for keys that exist in only one of data_changes and modem_changes :
append data to the same file "file.txt" key--> data_changes_values or key-->modem_changes values
EXPECTED OUTPUT:-
Create a text file with the following data
305403 302180
312994 311957 253036 312591
311957 312621
313113
以下是我的嘗試,但不是我的服務宗旨......
build_dep_list= [i
for k, v in itertools.chain.from_iterable(d.iteritems() for d in (data_changes, modem_changes))
for i in [k] + (v or [])
if i]
print "BUILD LIST"
print list(set(build_dep_list))
CURRENT OUTPUT:-
['305403', '302180', '313113', '311957', '312621', '253036', '312994', '312591']
設置鍵的集合,然後你可以很容易地得到交集和對稱的差異等等:http://docs.python.org/2/library/stdtypes.html#set然後只需寫入文件。 – Patashu 2013-05-02 03:02:39