我是python新手,学习类和对象
我有一个文件,其中包含大量csv格式的口袋妖怪数据-示例如下:
Number,Name,Type1,Type2,HP,Attack,Defense,SpecialAtk,SpecialDef,Speed,Generation,Legendary,Mega
1,Bulbasaur,Grass,Poison,45,49,49,65,65,45,1,FALSE,FALSE
2,Ivysaur,Grass,Poison,60,62,63,80,80,60,1,FALSE,FALSE
3,Venusaur,Grass,Poison,80,82,83,100,100,80,1,FALSE,FALSE
6,Mega Charizard Y,Fire,Flying,78,104,78,159,115,100,1,FALSE,TRUE
10,Caterpie,Bug,,45,30,35,20,20,45,1,FALSE,FALSE
11,Metapod,Bug,,50,20,55,25,25,30,1,FALSE,FALSE
12,Butterfree,Bug,Flying,60,45,50,90,80,70,1,FALSE,FALSE
13,Weedle,Bug,Poison,40,35,30,20,20,50,1,FALSE,FALSE
14,Kakuna,Bug,Poison,45,25,50,25,25,35,1,FALSE,FALSE
20,Raticate,Normal,,55,81,60,50,70,97,1,FALSE,FALSE
我已经定义了我的口袋妖怪类并打开了以下文件:
pokedex = open('../resource/lib/public/pokedex.csv', 'r')
for line in pokedex:
row = line.strip().split(",")
class Pokemon:
def __init__(self, Number, Name, Type1, Type2 = "" , HP, Attack, Defense,
SpecialAtk, SpecialDef, Speed,Generation, Legendary, Mega):
self.Number = Number
self.Name = Name
self.Type1 = Type1
self.Type2 = Type2
self.HP = HP
self.Attack = Attack
self.Defense = Defense
self.SpecialAtk = SpecialAtk
self.SpecialDef = SpecialDef
self.Speed = Speed
self.Generation = Generation
self.Legendary = Legendary
self.Mega = Mega
def total_stats(self):
total = self.HP+self.Attack+self.Defense+self.SpecialAtk+self.SpecialDef+self.Speed
return total
我想用这些数据回答一系列问题,例如:
- What Pokemon has the highest HP statistic?
- Excluding Pokemon that are either Mega or Legendary, what Pokemon has the highest Defense statistic?
- Among Legendary Pokemon, what is the most common type? Include both Type1 and Type2 in your count.
-In terms of the sum of all six stats (HP, Attack, Defense, Special Attack, Special Defense, and Speed), what is the weakest Legendary Pokemon? If there is a tie, list any of the tying Pokemon.
我该怎么做呢?我不知道如何将文件中的数据链接到口袋妖怪类。请帮忙
您可以将数据存储在列表中This isn't something new。那么,你会这样做吗
在控制台中,我们将看到如下内容
如果我们想看看口袋妖怪列表变量是什么
单击以查找索引为3的列表项中的实例
正如您所提到的,要想让口袋妖怪拥有最高的HP,您需要使用
operator.attrgetter()
来获得该值如果我们调整代码以包含此部分
我们将在控制台中获得以下内容
所以我们可以看到列表max_ind只有一个值
最大生命是80,拥有它的口袋妖怪是维努索
要排除超级或传奇的口袋妖怪,请从here获得灵感,并将其与前面提到的内容结合起来
您可以通过创建一个实例来完成
相关问题 更多 >
编程相关推荐