简体   繁体   English

Python arrays:为什么是 output 3

[英]Python arrays: why is the output 3

Can someone explain me why the output of the variable a is 3?有人能解释一下为什么变量 a 的 output 是 3 吗?

a = [4,3,1][1]

(Output) 3 (输出)3

You are selecting the first (array count starts at 0) element of your array [4, 3, 1], which is 3.您正在选择数组 [4, 3, 1] 的第一个(数组计数从 0 开始)元素,即 3。

To be easier to understand, think of that line of code as split into two:为了更容易理解,将该行代码分为两部分:

list = [4,3,1]
a = list[1]

You are indexing the 1st position (indexing starts at 0), which is the value 3 in the list you defined.您正在索引第一个 position(索引从 0 开始),即您定义的列表中的值 3。

You are defining an array [4, 3, 1] .您正在定义一个数组[4, 3, 1] By writing [1] you are selecting the second element of this array (the arrays in python start from 0).通过写入[1] ,您正在选择该数组的第二个元素(python 中的 arrays 从 0 开始)。 So a equals to the second element of array - 3. If you write [0] or [2] you get 4 or 1 accordingly.所以a等于数组的第二个元素 - 3。如果你写 [0] 或 [2],你会相应地得到 4 或 1。

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

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