简体   繁体   English

Python列表索引,列表[[x] [y]:-z]

[英]Python list Indexing, List[ [x][y]:-z ]

I'm somewhat new to Python. 我是Python的新手。 I have the following python 2 code: 我有以下python 2代码:

m = [1,1,7,7,0,0,0,0,1,6,6,9,7,9,2] randomDigs = m[6 - [3][0]: -1]

What is happening inside randomDigs? randomDigs内部发生了什么? I get the following output: 我得到以下输出:

[7, 0, 0, 0, 0, 1, 6, 6, 9, 7, 9]

[3][0] is just a complicated way to write 3 (the first element in the list containing 3 as only element). [3][0]只是一种写3的复杂方法(列表中的第一个元素仅包含3 )。 so your code boils down to m[3:-1] which means: create a new list from m starting from the 3rd (note: list indices start at 0) element up to (and excluding) the last element. 因此您的代码可以归结为m[3:-1] ,这意味着:从第3个(注意:列表索引从0开始)元素直至(并排除)最后一个元素从m开始创建一个新列表。

this answer explains how list indexing and slicing works in great detail. 此答案详细说明了列表索引编制和切片的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM