2011-08-31 101 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

在答案中已修复,谢谢! –