无法在Python中反转列表,从而获取"Nonetype"作为清单 [英] Unable to reverse lists in Python, getting "Nonetype" as list
问题描述
我有一个.py
文件,该文件带有一个列表,查找最低编号,将其放入新数组中,从第一个数组中删除最低编号,然后重复执行,直到原始数组返回不再包含更多项目为止:>
I have a .py
file that takes a list, finds the lowest number, puts it into a new array, removes the lowest number from the first array, and repeats until the original array returns contains no more items:
def qSort(lsort):
listlength = len(lsort)
sortedlist = list()
if listlength == 0:
return lsort
else:
while listlength > 0:
lmin = min(lsort)
sortedlist.append(lmin)
lsort.remove(lmin)
listlength = len(lsort)
return sortedlist
现在,另一个.py
文件将导入qSort
并将其在某些列表上运行,并将其保存到变量中.然后,我尝试在列表上使用.reverse()
命令,最终得到了NoneType
的身份.我尝试使用reversed()
,但所做的只是说"<listreverseiterator object at 0xSomeRandomHex>"
:
Now another .py
file imports the qSort
and runs it on some list, saving it to a variable. Then I try to use the .reverse()
command on the list and I end up getting it as a NoneType
. I try to use reversed()
, but all it does is say "<listreverseiterator object at 0xSomeRandomHex>"
:
from qSort import qSort #refer to my first Pastebin
qSort = qSort([5,42,66,1,24,5234,62])
print qSort #this prints the sorted list
print type(qSort) #this prints <type 'list'>
print qSort.reverse() #this prints None
print reversed(qSort) #this prints "<listreverseiterator object at 0xSomeRandomHex>"
任何人都可以解释为什么不管我做什么我似乎都无法撤消名单吗?
Can anyone explain why I can't seem to reverse the list, no matter what I do?
推荐答案
正如jcomeau所提到的,.reverse()
函数会在适当的位置更改列表.它不会返回列表,而是会更改qSort
.
As jcomeau mentions, the .reverse()
function changes the list in place. It does not return the list, but rather leaves qSort
altered.
如果您要返回"已反转的列表,因此可以像在示例中尝试的那样使用它,则可以以-1的方向进行切片
If you want to 'return' the reversed list, so it can be used like you attempt in your example, you can do a slice with a direction of -1
因此将print qSort.reverse()
替换为print qSort[::-1]
您应该了解切片及其有用的内容.我没有真正在本教程中看到一个同时描述所有地方的地方,( http ://docs.python.org/tutorial/introduction.html#lists 并未真正涵盖所有内容),因此希望这里有一些说明性示例.
You should know slices, its useful stuff. I didn't really see a place in the tutorial where it was all described at once, (http://docs.python.org/tutorial/introduction.html#lists doesn't really cover everything) so hopefully here are some illustrative examples.
语法是:a[firstIndexInclusive:endIndexExclusive:Step]
>>> a = range(20)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> a[7:] #seventh term and forward
[7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> a[:11] #everything before the 11th term
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[::2] # even indexed terms. 0th, 2nd, etc
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> a[4:17]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> a[4:17:2]
[4, 6, 8, 10, 12, 14, 16]
>>> a[::-1]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> a[19:4:-5]
[19, 14, 9]
>>> a[1:4] = [100, 200, 300] #you can assign to slices too
>>> a
[0, 100, 200, 300, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
这篇关于无法在Python中反转列表,从而获取"Nonetype"作为清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!