为什么 python reduce() 跳过 None 元素? [英] Why python reduce() skip over the None element?

查看:41
本文介绍了为什么 python reduce() 跳过 None 元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取一个 url 的主站点.首先我使用了一个 for 循环并实现了目标.

I tried to get the home site of a url. Firstly I used a for loop and achieve the goal.

home = ''
my_url = 'http://www.mysite.com/subdir/subdir2/index.html'
for item in my_url.split('/')[:3]:
    home += item + '/'
print  home

我可以得到

'http://www.mysite.com/' 

然后我遇到了我以前从未使用过的 reduce().所以我试了一下,这是代码:

Then I come across reduce() which I had never used before. So I get it a shot,here is the code:

my_url = 'http://www.mysite.com/subdir/subdir2/index.html'
home = ''
home = reduce(lambda x,y : x + y + '/',my_url.split('/')[:3])
print home

这次我得到了

'http:/www.mysite.com/'

是reduce只是省略了其中的None吗?结果的解释是什么?

Is it reduce just ommit the None in it?What's the explanation for the result?

是的,我从这个主题知道我只能用urllib的解析器函数来做,所以我希望这里的讨论集中在reduce()

yeah,I know from this topic that I could just use urllib's parser function to do it,so I hope here the discussion be focused on the reduce()

推荐答案

my_url = 'http://www.mysite.com/subdir/subdir2/index.html'
home = ''
home = reduce(lambda x,y : x + y + '/',my_url.split('/')[:3])

my_url.split('/')[:3] #=> ['http:', '', 'www.mysite.com']

'http:' + '' + '/' #=> 'http:/'
'http:/' + 'www.mysite.com' + '/' #=> 'http:/www.mysite.com/'

这并不神秘.一切都按预期工作 - 问题在于 URL 不统一,因为协议用双斜杠分隔.

This is not mysterious. Everything works as expected - the problem is that URLs are not uniform, in that the protocol is separated with a double slash.

functional 中的 scanl(http://pypi.python.org/pypi/functional):

In [11]: home = scanl(lambda x,y : '%s%s/'%(x,y),my_url.split('/')[0],my_url.split('/')[1:3])

In [12]: home
Out[12]: <generator object _scanl at 0x0000000003DEC828>

In [13]: list(home)
Out[13]: ['http:', 'http:/', 'http:/www.mysite.com/']

请注意,str.join 实现的算法略有不同:

Note that str.join implements a slightly different algorithm:

In [16]: '/'.join(my_url.split('/'))
Out[16]: 'http://www.mysite.com/subdir/subdir2/index.html'

这就是人们通常想要的——它相当于:

This is what people usually want - it is equivalent to:

In [22]: reduce(lambda x,y : '%s/%s'%(x,y),my_url.split('/'))
Out[22]: 'http://www.mysite.com/subdir/subdir2/index.html'

这篇关于为什么 python reduce() 跳过 None 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆