简体   繁体   English

检查列表中是否多次存在?

[英]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: 因此amt是介于1和3之间的数字,它决定要面对的怪物数量,然后它通过字典匹配值的第一个索引,然后将怪物附加到列表中,最终看起来像这样:

[['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 . 并且如果您想以某种方式与它们区别,最好创建一个怪物详细信息类,那么如果重复它们,则为每个怪物更改一些参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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