2015-09-27 38 views
1

輸出文件我有一個像如下寫在巨蟒

list1 = [['abc',{2:1,3:4,5:6}],['xyz',{4:0,9:5,7:8}],.......]] 

我希望寫在AO/P文件清單的格式如下列表:

'abc' 2:1 3:4 5:6 
'xyz' 4:0 9:5 7:8 

我嘗試不同的方法,但沒能得到它在上面format.Let我還提到,len(list1) = 30000。請給我建議以優化的方式

+4

所以你想輸出只有一個長線?順便說一句,請張貼您使用的代碼,以便我們可以幫助您,而不是我們爲您編寫代碼。 – dopstar

回答

2
list1 = [['abc', {2: 1, 3: 4, 5: 6}], ['xyz', {4: 0, 9: 5, 7: 8}]] 

with open('some.txt', 'w') as output_file: 
    for k, v in list1: 
     output_file.write('\'{}\' {}\n'.format(
      k, ' '.join('{}:{}'.format(key, val) for key, val in v.items()))) 

對於Python 3,請使用v.items()