开发者

Unable to reverse lists in Python, getting "Nonetype" as list [duplicate]

This question already has answers here: Why do these list methods (append, sort, extend, remove, clear, reverse) return None rather than the resulting list? (6 answers) Closed 6 months ago.

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:
        re开发者_C百科turn lsort
    else:
        while listlength > 0:
            lmin = min(lsort)
            sortedlist.append(lmin)
            lsort.remove(lmin)
            listlength = len(lsort)
        return sortedlist

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?


As jcomeau mentions, the .reverse() function changes the list in place. It does not return the list, but rather leaves qSort altered.

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

So replace print qSort.reverse() with print qSort[::-1]


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.

Syntax is: 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]


list.reverse() reverses in-place and returns nothing (None). so you don't say:


mylist = mylist.reverse()

you say:


mylist.reverse()

or alternatively:


mylist = list(reversed(mylist))


The reverse() list method sorts the list in place and returns None to remind you of that (according to note 7 in the documentation). The built-in reversed() function returns an iterator object, which can be turned into a list object by passing it to the list() constructor function like this: list(reversed(qSort)). You can accomplish the same thing by creating a slice with a step size of negative one so it goes backwards, i.e qSort[::-1].

BTW, list's also have a sort() method (but be careful, it also returns None ;-).


l5= [60,70,77]

myl2 = list(reversed(l5))

print(myl2)

or

mylist2 =[50,60,80,90]

mylist2.reverse()

print(mylist2)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜