简体   繁体   English

将元素的值分配给变量输出

[英]Assigning value of element to the variable output

So, I'm on a beginner's python course on coursera and there is a question I just don't understand:所以,我在coursera上学习初学者的python课程,有一个我不明白的问题:

Assign the value of the 34th element of lst to the variable output .lst的第 34 个元素的值赋给变量output

Here is the list:这是清单:

1st = ["hi", "morning", "dog", "506", "caterpillar", "balloons", 106, "yo-yo", "python",
       "moon", "water", "sleepy", "daffy", 45, "donald", "whiteboard", "glasses",
       "markers", "couches", "butterfly", "100", "magazine", "door", "picture", "window",
       ["Olympics", "handle"], "chair", "pages", "readings", "burger", "juggle", "craft",
       ["store", "poster", "board"], "laptop", "computer", "plates", "hotdog", "salad",
       "backpack", "zipper", "ring", "watch", "finger", "bags", "boxes", "pods", "peas",
       "apples", "horse", "guinea pig", "bowl", "EECS"]

What am I supposed to do with this thing?这东西我该怎么办? I'm not looking for anyone to straight up give me the answer, but rather help me understand what the question wants from me and how to solve it.我不是在寻找任何人直接给我答案,而是帮助我了解问题对我的要求以及如何解决它。

output = 1st[34]

“所以这就是它所说的,通过索引列表,您可以获得值并将其分配给变量输出”

To index a list, the correct syntax is:要索引列表,正确的语法是:

list[i] where the i is the index of the element you want to retrieve. list[i]其中i是您要检索的元素的索引。 So in your case:所以在你的情况下:

output = 1st[34]

Good luck with Corsera!祝 Corsera 好运!

yeah.是的。 i ended up figuring it out on my own.我最终自己弄清楚了。 but a big thanks to both of you, regardless.但无论如何,非常感谢你们俩。

so yeah, I did this (in case anyone else ever gets stuck on something like that as well):所以是的,我这样做了(以防其他人也被困在这样的事情上):

you need to find index 34 because python is 0 index你需要找到索引 34 因为 python 是 0 索引

output of that will be "laptop"输出将是“笔记本电脑”

print(lst[34])

now you need to assign the value "laptop" (35th element) to the variable "output"现在您需要将值“laptop”(第 35 个元素)分配给变量“output”

which looks like:看起来像:

output = "laptop"
print(output)

The indexing of the list starts from 0.列表的索引从 0 开始。
Therefore, to access the 34th element in the list, you need to enter the index 33 .因此,要访问列表中的第 34 个元素,您需要输入索引33
The code should then be like this:代码应该是这样的:

1st = ["hi", "morning", "dog", "506", "caterpillar", "balloons", 106, "yo-yo", "python",
       "moon", "water", "sleepy", "daffy", 45, "donald", "whiteboard", "glasses",
       "markers", "couches", "butterfly", "100", "magazine", "door", "picture", "window",
       ["Olympics", "handle"], "chair", "pages", "readings", "burger", "juggle", "craft",
       ["store", "poster", "board"], "laptop", "computer", "plates", "hotdog", "salad",
       "backpack", "zipper", "ring", "watch", "finger", "bags", "boxes", "pods", "peas",
       "apples", "horse", "guinea pig", "bowl", "EECS"];
output = lst[33];

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

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