I am actually new to python and just started learning it i am working with an module which is based on Pokeapi and its name is Pokebase which is basically a python wrapper for that api so I am having a problem with it: Here's a for loop in which I print some data recieved by the api. Code:
import pokebase as pb
p1=pb.pokemon('charmander')
for stat in p1.stats:
print('{}: {}'.format(stat.stat.name, stat.base_stat))
actually I dont want to print this value I want to save this value in a variable or list how can i do that?
If you want to save it in a list:
[[stat.stat.name, stat.base_stat] for stat in p1.stats]
This is called alist comprehension .
If you want to use a dictionary:
{stat.stat.name: stat.base_stat for stat in p1.stats}
This should do it!
import pokebase as pb
p1=pb.pokemon('charmander')
names = []
base_stats = []
for stat in p1.stats:
#print('{}: {}'.format(stat.stat.name, stat.base_stat))
names.append(stat.stat.name)
base_stats.append(stat.base_stat)
This should do the trick.
import pokebase as pb
p1 = pb.pokemon('charmander')
stats = []
for stat in p1.stats:
stats.append( '{}: {}'.format(stat.stat.name, stat.base_stat) )
Something like this, I guess:
stat_str = ""
for stat in p1.stats:
stat_str += f"{stat.stat.name}: {stat.base_stat}\n"
I'm a Python Noob and there is undoubtedly a better solution but this works:
import pokebase as pb
p1 = pb.pokemon('charmander')
p1Dictionary = {}
for stat in p1.stats:
p1Dictionary.update({stat.stat.name : stat.base_stat})
print(p1Dictionary)
for key, value in p1Dictionary.items():
print(key, value)
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.