关于Python中列表的基本问题 [英] A basic question about list in Python

查看:63
本文介绍了关于Python中列表的基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

L = [1,2,3,4,5]



print L [4:1] #prints一个空列表。不能反向移动。



L [:: - 1]如何反向移动?



我尝试过:



尝试通过不同的索引/切片来理解python中的列表遍历。

L=[1,2,3,4,5]

print L[4:1] #prints an empty list. Can not traverse in reverse direction.

How does L[::-1] can traverse in reverse direction?

What I have tried:

Trying to understand list traversal in python by different indices/slicing.

推荐答案

请参阅 3。 Python非正式简介 - 列表 [ ^ ]。



[edit]

缺少的开始和结束值表示它应该使用整个列表。将步长值设置为-1会导致值被反转,因此将其视为

See 3. An Informal Introduction to Python — Lists[^].

[edit]
The missing start and stop values indicate that it should use the entire list. Setting a step value of -1 causes the values to be reversed, so it treats it as
list[end:start:step]



但是,这仅适用于具有 __ reverse __()方法的类型。它只是调用 list.reverse(); 的简写方式。

[/ edit]


However, this only works for types that have a __reversed__() method. It is just a shorthand way of calling list.reverse();.
[/edit]


这篇关于关于Python中列表的基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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