Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中列表的颠倒顺序_Python_List - Fatal编程技术网

Python中列表的颠倒顺序

Python中列表的颠倒顺序,python,list,Python,List,我得到了以下代码: def two_pair(ranks): """If there are two pair, return the two ranks as a tuple: (highest, lowest); otherwise return None.""" pair = kind(2,ranks) lowpair = kind(2, list(reversed(ranks))) if pair and lowpair != pair:

我得到了以下代码:

def two_pair(ranks):
    """If there are two pair, return the two ranks as a
    tuple: (highest, lowest); otherwise return None."""
    pair = kind(2,ranks)
    lowpair = kind(2, list(reversed(ranks)))
    if pair and lowpair != pair:
        return (pair,lowpair)
    else:
        return None
在lowpair变量中,为什么需要说明
list()
?为什么你不能直接说
反向(等级)
<代码>等级是一个列表。这不是已经暗示了吗

返回迭代器,而不是列表。我们需要显式地将其转换为一个列表,除非我们只想迭代它

a = [1, 2, 3]
print reversed(a)        # <listreverseiterator object at 0x7fc57d746790>
返回迭代器,而不是列表。我们需要显式地将其转换为一个列表,除非我们只想迭代它

a = [1, 2, 3]
print reversed(a)        # <listreverseiterator object at 0x7fc57d746790>
反向(等级)
不是反向列表。它是一个迭代器:

>>> reversed([1, 2, 3])
<listreverseiterator object at 0x0000000001DDE9E8>
>>反向([1,2,3])
list
调用是获取反向列表所必需的。

reversed(ranks)
不是反向列表。它是一个迭代器:

>>> reversed([1, 2, 3])
<listreverseiterator object at 0x0000000001DDE9E8>
>>反向([1,2,3])

list
调用是获取反向列表所必需的。

如果您想要较短的代码,可以执行
列组[:-1]
而不是
列表(反向(列组))


如果您想要较短的代码,可以使用
列[:-1]
而不是
列表(反转(列))