Funday={"entertainment": [{ "game": "golf", "movies_name": [{ "movie": "Spiderman", "production": "marvel"}, { "movie": "endgame", "production": "marvel"}, { "movie": "justiceleague", "production": "DC" }, { "movie": "batman", "production": "DC"}]}]}
uj5u.com熱心網友回復:
movie = ["spiderman", "endgame", "justiceleague", "batman"]
for val in movie:
print(val)
uj5u.com熱心網友回復:
我相信您正在嘗試做的事情如下:
Funday={"entertainment": [{ "game": "golf", "movies_name": [{ "movie": "Spiderman", "production": "marvel"}, { "movie": "endgame", "production": "marvel"}, { "movie": "justiceleague", "production": "DC" }, { "movie": "batman", "production": "DC"}]}]}
movies = []
for entertainment in Funday["entertainment"]:
for movie in entertainment["movies_name"]:
movies.append(movie["movie"])
在第一行中,我初始化了電影串列。在第二個中,我遍歷每個娛樂值(其中只有一個,但我假設您有多個)。之后,我回圈播放每部電影,并將它們添加到movies.
但是,您的動機在這里并不十分清楚。首先,為什么娛樂是一個清單?如果有多個游戲,那么您可能希望將movies每個娛樂值存盤在字典中:
movies = {}
for entertainment in Funday["entertainment"]:
for movie in entertainment["movies_name"]:
game = entertainment["game"]
if game not in movies:
movies[game] = []
movies[game].append(movie["movie"])
uj5u.com熱心網友回復:
lst=[]
for i in dct["entertainment"]:
if(i['movies_name']):
for j in i['movies_name']:
lst.append(j['movie'])
print(lst)
uj5u.com熱心網友回復:
我認為這會更干凈
Funday = {
"entertainment": [
{
"game": "golf",
"movies_name": [
{
"movie": "Spiderman",
"production": "marvel"
},
{
"movie": "endgame",
"production": "marvel"
},
{
"movie": "justiceleague",
"production": "DC"
},
{
"movie": "batman",
"production": "DC"
}
]
}
]
}
dict = Funday["entertainment"][0]["movies_name"]
movie = []
for value in dict:
movie.append(value["movie"])
print(movie)
# or exactly
print(f'"movie" = {movie}')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/512351.html
上一篇:控制39位字串中的變異作為遺傳演算法中的候選解決方案
下一篇:查找陣列中數字的乘積
