返回嵌套列表中的项目(python)
我正在用 Python 进行基础编程课程的实验,但我不知道如何在另一个列表中返回一个列表中的数字。
我应该用“一个表达式,没有括号”返回 4
[1,[2,[3,4]]]
有什么想法吗? 到目前为止我所得到的只是返回 [2,[3,4]] 并且我无法弄清楚除此之外的任何内容。
I'm doing a lab for a basic programming class in Python, and I can't figure out how to return a number in a list, in another list.
I'm supposed to return 4 with "one expression and no parenthesis"
[1,[2,[3,4]]]
Any ideas?
All I've gotten so far is returning [2,[3,4]] and I haven't been able to figure out anything past that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种表达:
One expression:
如果您不知道,要访问列表的最后一个元素,可以使用负索引。 a[-1] 表示倒数第一个元素。当您想要打印相对于末尾的位置的元素时,它非常有用。
假设您有一个不同的 a。
a = [1, 1, 1, 1, 1, 1,[1, 1, 1, 1, 2,[1, 1, 1, 3,4]]]
上面的代码仍然会给你最后一个元素。
In case you don't know, to access the last element of a list you can use negative index. a[-1] means the first element from the end. It's very useful when you want to print elements with position relative to the end.
Let's say you had a different a.
a = [1, 1, 1, 1, 1, 1,[1, 1, 1, 1, 2,[1, 1, 1, 3,4]]]
The code above will still give you the last element.