[0, 1, 2, 3, 4]
is a list of integers. It's a one-dimensional list and you only need one index to access its elements. For example, years[3]
is the integer 3
.
[[0],[2],[4],[6],[8]]
is a list of lists of integers, so it's a two-dimensional list. You need two indices to access its integer elements. For example, result[3]
will give you the list [6]
, and the zeroth index of that list will give you the integer 6
. In other words, result[3][0]
gives you the integer 6
.
The list comprehension result = [[2*i] for i in years]
is what creates the two-dimensional list because you asked it to. You said:
- For every
i
in years
,
- Calculate
2 * i
- Put that into a list
[2 * i]
- Collect all these lists of
[2 * i]
in a single list.
If you wanted a 1-d list, skip the brackets around [2 * i]
like so: result = [2 * i for i in years]
. This tells Python to:
- For every
i
in years
,
- Collect all these
2 * i
into a single list
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…