我注意到从函数返回列表(具有单个元素)然后尝试使用reverse()反转列表时出现奇怪的行为
我在这里蒸馏过:
def myFunction():
return ["The Smiths"]
nums = [5,4,3,2,1]
nums.reverse()
print nums # 1,2,3,4,5 - fine!
# lets use one element in a list
something = ["Gwen Stefani"]
something.reverse()
print something # ["Gwen Stefani"] also fine
# now let's do the same, but from a function
a = myFunction()
print a # The Smiths
print a.reverse() # None
print a[::-1] # The Smiths
我需要一个成年人来解释创建None而不是像[::-1]那样看到单个元素的过程。
答案 0 :(得分:2)
list.reverse()
就地反转列表,但函数本身不返回任何内容。
a = ["The Smiths"]
print a # The Smiths
print a.reverse() # None
print a # It's already there