好烦恼,经常有一些小问题卡住。
字符串的方法reverse()。为什么不能直接赋值给一个变量?a="abcde"
temp=list(a)
temp2=temp.reverse()
print(temp2)
结果显示"None"
a="abcde"
temp=list(a)
temp.reverse()
temp2=temp
print(temp2)
要这样才能赋到值给temp2,为什么呢? 本帖最后由 mgsky1 于 2018-2-11 10:17 编辑
不是不能赋值,而是列表的方法reverse()返回的不是列表,而是一个NoneType对象,它只是对自身序列的改变。
可以使用type这个BIF看返回值
http://xxx.fishc.com/album/201802/11/101531na99t1f04v8f8ugm.png
如果要赋值给temp2的话,可以使用copy这个内置方法,它会返回一个list 你对列表使用了此函数后,这个列表就倒过来了,并不用再一次赋值。
a="abcde"
temp=list(a)
temp.reverse()
print(temp)
print(reversed(temp))
print(temp)
页:
[1]