2011-08-31 107 views
1

基本上,我有以下形式的字典:迭代通過列表的Python列表,並創建一個新的列表

'the trip':('The Trip','www.SearchHero.com',"See sponsored listings for 'The Trip.' Click here!",0.1,'The Trip','3809466'), 
'post office':('Post Office','www.SearchHero.com',"See sponsored listings for 'Post Office.' Click here!",0.1,'Post Office','5803938'), 
'balanced diets':('Last Minute Deals','www.SearchHero.com',"See sponsored listings for 'Last Minute Deals.' Click here!",0.1,'Last Minute Deals','1118375') 

我要輸出形式的列表:

{'.1, 3809466', '.1, 5803938', '.1, '1118375'}

我是Python的新手,但在Ruby中我知道我會使用map函數。 python中的等價物是什麼?在此先感謝

回答

3

一個基本的例子:

['%f, %s' % (x,y) for _,_,_,x,_,y in d.values()] 

您可以調整格式字符串有超過輸出更多的控制。 底層語法結構被稱爲list comprehension

1
[', '.join((value[3], value[5])) for value in my_dict.iteritems()] 
+0

您在某處丟失了括號嗎? – skline

+0

在答案中已修復,謝謝! –