无法在 Python 中反转列表,获得“Nonetype"作为列表 [英] Unable to reverse lists in Python, getting "Nonetype" as list

查看:25
本文介绍了无法在 Python 中反转列表,获得“Nonetype"作为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .py 文件,它接受一个列表,找到最小的数字,将它放入一个新数组中,从第一个数组中删除最小的数字,并重复直到原始数组返回包含没有更多的项目:

def qSort(lsort):列表长度 = len(lsort)排序列表 = 列表()如果列表长度 == 0:返回排序别的:而列表长度 >0:lmin = min(lsort)sortedlist.append(lmin)lsort.remove(lmin)列表长度 = len(lsort)返回排序列表

现在另一个 .py 文件导入 qSort 并在某个列表上运行它,将其保存到一个变量中.然后我尝试使用列表中的 .reverse() 命令,最终将其作为 NoneType 获取.我尝试使用 reversed(),但它所做的只是说 "":

from qSort import qSort #引用我的第一个PastebinqSort = qSort([5,42,66,1,24,5234,62])print qSort #this 打印排序列表print type(qSort) #this 打印 打印 qSort.reverse() #this 打印无print reversed(qSort) #this 打印"

谁能解释为什么无论我做什么,我似乎都无法反转列表?

解决方案

正如 jcomeau 所提到的,.reverse() 函数就地更改了列表.它不返回列表,而是改变 qSort.

如果你想返回"反向列表,所以它可以像你在你的例子中尝试的那样使用,你可以做一个方向为 -1 的切片

因此将 print qSort.reverse() 替换为 print qSort[::-1]

<小时>

你应该知道切片,它很有用.我真的没有在教程中看到一个地方一次性描述所有内容,(http://docs.python.org/tutorial/introduction.html#lists 并没有真正涵盖所有内容)所以希望这里有一些说明性的例子.

语法为:a[firstIndexInclusive:endIndexExclusive:Step]

<预><代码>>>>a = 范围(20)>>>一种[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]>>>a[7:] #第七届及以后[7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]>>>a[:11] #第 11 学期之前的所有内容[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>>a[::2] # 偶数索引项.0号、2号等[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]>>>[4:17][4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]>>>[4:17:2][4, 6, 8, 10, 12, 14, 16]>>>[::-1][19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]>>>[19:4:-5][19, 14, 9]>>>a[1:4] = [100, 200, 300] #你也可以分配给切片>>>一种[0, 100, 200, 300, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

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

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]

这篇关于无法在 Python 中反转列表,获得“Nonetype"作为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆