为什么 python reduce() 跳过 None 元素?

编程入门 行业动态 更新时间:2024-10-27 13:32:50
本文介绍了为什么 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 = '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 元素?

本文发布于:2023-11-24 10:09:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跳过   元素   python   reduce

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!