简体   繁体   English

在python中打印2个列表

[英]Printing 2 lists in python

I'm trying to learn python and I was using an app. 我正在尝试学习python,并且正在使用一个应用程序。 In one of the tests it asked me for the output of: 在其中一项测试中,它要求我提供以下输出:

list = [1,1,2,3,5,8,13]
print(list[list[4]])

I couldn't find the solution and I looked at the solution, It was 8. But the problem is, it doesn't show you why. 我找不到解决方案,而是看了解决方案,当时是8。但是问题是,它没有告诉您原因。 I did a little search on this subject on google and stackoverflow but I couldn't find why. 我在谷歌和stackoverflow上对此主题做了一些搜索,但找不到原因。 Maybe I don't know how to ask this question. 也许我不知道怎么问这个问题。 Anyways, as I said I'm pretty new to this stuff so the title probably doesn't fit the question but I really don't know how to write my question in 5 words. 无论如何,正如我说的那样,我对这些东西还很陌生,因此标题可能不适合该问题,但我真的不知道该如何用5个单词来写我的问题。 It would be great if you help. 如果您有帮助,那就太好了。

You can work from the inside to the outside of these calls. 您可以从内部到外部进行这些调用。 That line is equivalent to the following: 该行等效于以下内容:

value1 = list[4]   # this is 5 
value2 = list[value1]   # this is 8
print(value2)

You want to find the value of List[List[4]] 您想找到List[List[4]]

First, let's substitute the inner part List[4] and it will equal 5 as we count from 0 in python so you have: 首先,让我们替换内部部分List[4] ,当我们从python中的0计数时,它等于5

list[0] = 1
list[1] = 1
list[2] = 2
list[3] = 3
list[4] = 5

and so on. 等等。

So now after getting the inner part we can easily figure out the overall question as follows: 因此,现在在获得内在部分之后,我们可以轻松地找出总体问题,如下所示:

 List[List[4]] = List[5] = 8

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

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