简体   繁体   中英

Check if something is in a list multiple times?

I have a dictionary of monsters, which I get enemies from by choosing a random integer and matching it to the first number in the dictionary value (it's a list)

#NAME: ID, HP, MP, ATK, DEF, MGATK, MGDEF, SPEED, EXPSHARE, IMG
monsters_list = {"BOXROACH": [1, 20, 5, 5, 5, 5, 5, 5, 5, pg.image.load("boxcroach.png")],

for i in range(amt):
            monster = random.randint(1, len(self.monsters))
            for key in self.monsters:
                if self.monsters[key][0] == monster:
                    monsters.append([key, self.monsters[key]])

So amt is a number between 1 and 3 which decides how many monsters will be facing, and then it goes through the dictionary to match the first index of the value and then append the monster to a list, which ends up looking like this:

[['SLUGGER', [2, 20, 10, 5, 3, 8, 7, 11, 7, <Surface(150x160x32 SW)>]], ['SLUGGER', [2, 20, 10, 5, 3, 8, 7, 11, 7, <Surface(150x160x32 SW)>]], ['MEAN SPIRIT', [10, 15, 15, 5, 5, 5, 5, 5, 20, <Surface(100x100x32 SW)>]]]

(ignore the dumb names)
What I want to do is to check if there are multiple of the same enemy, and then differentiate them in some way. I was originally going to go through to add all the indexes to another list and then compare them, but is there a more efficient way to do this? Thanks.

You can do by: check your random numbers in order to not be the same so your monsters will be different.

if an random number repeated again do next random number.

and also if you want to different them in some ways it's better to make a monster details class then if they were repeated then change some parameters on there for each monster .

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