簡體   English   中英

將元素的值分配給變量輸出

[英]Assigning value of element to the variable output

所以,我在coursera上學習初學者的python課程,有一個我不明白的問題:

lst的第 34 個元素的值賦給變量output

這是清單:

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 = 1st[34]

“所以這就是它所說的,通過索引列表,您可以獲得值並將其分配給變量輸出”

要索引列表,正確的語法是:

list[i]其中i是您要檢索的元素的索引。 所以在你的情況下:

output = 1st[34]

祝 Corsera 好運!

是的。 我最終自己弄清楚了。 但無論如何,非常感謝你們倆。

所以是的,我這樣做了(以防其他人也被困在這樣的事情上):

你需要找到索引 34 因為 python 是 0 索引

輸出將是“筆記本電腦”

print(lst[34])

現在您需要將值“laptop”(第 35 個元素)分配給變量“output”

看起來像:

output = "laptop"
print(output)

列表的索引從 0 開始。
因此,要訪問列表中的第 34 個元素,您需要輸入索引33
代碼應該是這樣的:

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