我试图获取一个 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 = 'www.mysite/subdir/subdir2/index.html' for item in my_url.split('/')[:3]: home += item + '/' print home我可以得到
'www.mysite/'然后我遇到了我以前从未使用过的 reduce().所以我试了一下,这是代码:
Then I come across reduce() which I had never used before. So I get it a shot,here is the code:
my_url = 'www.mysite/subdir/subdir2/index.html' home = '' home = reduce(lambda x,y : x + y + '/',my_url.split('/')[:3]) print home这次我得到了
'http:/www.mysite/'是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 = 'www.mysite/subdir/subdir2/index.html' home = '' home = reduce(lambda x,y : x + y + '/',my_url.split('/')[:3]) my_url.split('/')[:3] #=> ['http:', '', 'www.mysite'] 'http:' + '' + '/' #=> 'http:/' 'http:/' + 'www.mysite' + '/' #=> 'http:/www.mysite/'这并不神秘.一切都按预期工作 - 问题在于 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(pypi.python/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/']请注意,str.join 实现的算法略有不同:
Note that str.join implements a slightly different algorithm:
In [16]: '/'.join(my_url.split('/')) Out[16]: 'www.mysite/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]: 'www.mysite/subdir/subdir2/index.html'更多推荐
为什么 python reduce() 跳过 None 元素?
发布评论