简体   繁体   中英

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

I am at a loss as to how I can iterate over each value in the list below. For instance i want to print the "names" only, how would I do that ?

The list:

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", ]
}]

The best way I can think of is to try using a nested for loop to get access for all the "name" values only but I get a "TypeError: string indices must be integers".I want to be able to pass my LIST to my HTML page and display the values there for "name" "poster" and "summary" there.

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

Check the data inside the List, after carefully examining the data, you miss a " sign in flyer 9

You literally create a dictionary in your List[0], you can pass the List [0] to a new dictionary

newDictionary = dict(List[0])

then you can access the names by using get method

print(newDictionary.get("name"))

or just convert that list into a dictionary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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