在Python中将列表存储在列表中 [英] Storing lists within lists in Python

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

问题描述

我对访问列表中的元素有疑问.

I have a question about accessing elements in lists.

这是代码:

movies = ["The Holy Grail", 1975, "Terry Jones and Terry Gilliam", 91,
          ["Graham Champman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]]

print(movies[4][1][3])

这是输出: Eric Idle

我的问题是,为什么输出Eric Idle?4代表什么,1和3代表什么?我很困惑.

My question is why is the output Eric Idle? What does 4 represent, what do 1 and 3 represent? I'm so confused.

推荐答案

您的列表分为多个值.

# movies: values
0. "The Holy Grail"
1. 1975
2. "Terry Jones and Terry Gilliam"
3. 91
4. ["Graham Champman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]

/!\索引从0开始

最后一个值也分为几个值:

The last value is also separated into values:

# movies[4]: values
0. "Graham Champman"
1. ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]

最后一个值也分为其他值:

And the last value is also separated into other values:

# movies[4][1]: values
0. "Michael Palin",
1. "John Cleese"
2. "Terry Gilliam"
3. "Eric Idle"
4. "Terry Jones"


因此,调用 movies [4] 会返回 movies 的最后一个元素:


So calling movies[4] returns the last element of movies:

["Graham Champman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]

键入电影[4] [1] 会返回以下内容:

Typing movies[4][1] returns this:

["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]

并键入电影[4] [1] [3] 会返回:

And typing movies[4][1][3] returns that:

"Eric Idle"

树状视图

movies
 0. | "The Holy Grail"
 1. | 1975
 2. | "Terry Jones and Terry Gilliam"
 3. | 91
 4. |____
    4.0. | "Graham Champman"
    4.1. |____
        4.1.0 | "Michael Palin"
        4.1.1 | "John Cleese"
        4.1.2 | "Terry Gilliam"
        4.1.3 | "Eric Idle"
        4.1.4 | "Terry Jones"

希望有帮助.

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

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