簡體   English   中英

如何訪問下面嵌套的 python 列表的各個值? 循環遍歷我的列表以獲取它們的最佳方法是什么?

[英]How can I access the individual values my nested python list below? Whats the Best way to loop over my list to get them?

我不知道如何迭代下面列表中的每個值。 例如,我只想打印“名稱”,我該怎么做?

列表:

List = [{

'name': ['NARUTO×UT', 'Naruto, the Genie, and the Three Wishes, Believe It!', 'Naruto SD: Rock Lee no Seishun Full-Power Ninden Mō Iccho', 'Naruto Shippūden: The Burning Chūnin Exam! Naruto vs. Konohamaru!!', 'Naruto: The Cross Roads', 'Naruto Spin-Off: Rock Lee & His Ninja Pals', 'Naruto Shippūden: Shippū! "Konoha Gakuen" Den', 'Naruto Shippūden: Blood Prison', 'Naruto Shippūden: The Lost Tower', 'Naruto Shippūden: The Will of Fire', 'Naruto Shippūden the Movie: Bonds', 'Naruto Shippūden', 'Naruto: Konoha Sports Festival', 'Naruto Shippūden', 'Naruto the Movie: Guardians of the Crescent Moon Kingdom', 'Naruto', 'Naruto the Movie: Legend of the Stone of Gelel', 'Naruto Special: Battle at Hidden Falls. I Am the Hero!', 'Naruto: Akaki Yotsuba no Clover o Sagase', 'Naruto the Movie: Ninja Clash in the Land of Snow', 'Naruto'], 

'poster': ["flyer 1","flyer 2", "flyer 3", "flyer 4", "flyer 5","flyer 6" "flyer 7", "flyer 8", "flyer 9", "flyer 10", "flyer 11", "flyer 12", "flyer 13", "flyer 14"], 

'summary': ["plot 1", "plot 2", "plot 3", "plot 4", "plot 5", "plot 6", "plot 7", "plot 8","plot 9", "plot 10", "plot 11", "plot 12","plot 13", "plot 14", ]
}]

我能想到的最好方法是嘗試使用嵌套的 for 循環來僅訪問所有“名稱”值,但我得到一個“類型錯誤:字符串索引必須是整數”。我希望能夠將我的 LIST 傳遞給我的 HTML 頁面並在那里顯示“名稱”“海報”和“摘要”的值。

for anime in list:
    for show in anime:
        print(show["name"])

查看List里面的數據,仔細查看數據后,你錯過了一個“簽到傳單”9

您可以在 List[0] 中創建字典,您可以將 List [0] 傳遞給新字典

newDictionary = dict(List[0])

然后您可以使用 get 方法訪問名稱

print(newDictionary.get("name"))

或者只是將該列表轉換為字典

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM