最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中List变量reverse后,结果为None

Python crifan 6352浏览 0评论

【问题】

Python中一个List变量allPageUrlList:

allPageUrlList=[‘http://blog.csdn.net/aomandeshangxiao/article/details/7449973′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7311564′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7452890′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7444095′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7418705′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7415618′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7399015′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7397697′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7395526′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7378810′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7328045′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7325383′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7302343′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7298840′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7286354′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7282272′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7220475′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7200852′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7196921′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7183842′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7163457′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7110045′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7096748′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7088612′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7081099′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7040515′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7040486′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7030200′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7022826′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7008636′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7006211′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7000077′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/7000463′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6988326′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6978681′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6966642′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6951161′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6948662′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6941724′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6940941′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6940670′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6938729′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6938676′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6929500′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6910643′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6910533′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6901958′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6830481′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6804814′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6788000′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6782227′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6775436′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6775055′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6773742′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6768249′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6767423′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6762856′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6759576′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6757619′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6741289′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6740122′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6737370′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6723649′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6718938′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6715688′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6672644′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6669626′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6668725′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6663119′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6662535′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6643831′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6659346′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6659305′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6654386′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6651617′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6638508′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6638460′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6632366′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6616935′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6608569′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6608101′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6606314′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6605186′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6603374′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6600725′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6598051′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6597866′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6597302′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6589510′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6587578′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6587497′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6586261′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6586240′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6586216′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6585687′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6572472′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6571991′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6427292′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6316155′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/6046319′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/5990612′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/5982862′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/5974408′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/5945053′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/5765979′, ‘http://blog.csdn.net/aomandeshangxiao/article/details/5387012’]

调用reverse后,结果不是期望的,颠倒过来的list,却是None:

print "reversedPageUrlList=",reversedPageUrlList;

值为:

reversedPageUrlList= None

【解决过程】

1.找了下,结果只找到这个官方解释:

5.6.4. Mutable Sequence Types

s.reverse() reverses the items of s in place

Note:The sort() and reverse() methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don’t return the sorted or reversed list.

但是什么是side effect,还是不懂。

2.后来找到:a=[1,2,3,4].reverse() – why "a" is None?,看到其讨论说到:

b = [1,2,3,4]
a = b.reverse()
would change the value of b.

才想起来,原来这个reverse函数,针对列表的操作,其结果是直接改变列表本身(为了节省空间),所以,直接就把原先的list改为你所想要的reversed后的结果了,而返回值,是空的,不返回任何值。

所以,本身直接使用:

allPageUrlList.reverse(); # -> is OK, the self is reversed !!!

即可实现把allPageUrlList翻转。

【总结】

对于List等Sequence等类型的变量,比如此处的List变量,其内置函数reverse,是直接操作变量本身,调用reverse后,变量本身的值就是reverse后的值了,所以不能出现:

reversedList = orignalList.reverse();

的用法,而应该是:

orignalList.reverse();
reversedList = orignalList;

此原则,同样适用于sort函数。

转载请注明:在路上 » 【已解决】Python中List变量reverse后,结果为None

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

    92 queries in 0.208 seconds, using 22.11MB memory