简体   繁体   English

如何比较两个不同大小的列表,找到匹配项并在两个列表中返回这些匹配项的索引

[英]How do I compare two lists of diffrent sizes, find matches and return the indices of those matches in both lists

In pygame I am building a GA simulation.在 pygame 中,我正在构建一个 GA 模拟。 The simulation involves 10 creatures and 100 pieces of food.模拟涉及 10 种生物和 100 种食物。 I am creating them as sprites.我将它们创建为精灵。 I tried to use built in sprite collision detection but I need to detect collisions between unique sprites, not sprites and groups or groups and groups.我尝试使用内置的精灵碰撞检测,但我需要检测独特精灵之间的碰撞,而不是精灵和组或组和组之间的碰撞。 That led me to realize I need a custom solution.这让我意识到我需要一个定制的解决方案。 Since my numbers are low I figured I could throw the positions of the food and creatures into lists and compare them.由于我的数字很低,我想我可以将食物和生物的位置放入列表中并进行比较。 That part works but what I can't figure out is how to get the inidices for the matches in the lists.那部分有效,但我不知道如何获取列表中匹配项的索引。 I'm not too worried about speed because I think my comparison is fast enough, especially given the small amount of data involved and the relative rarity of collisions but I'd still like an inexpensive solution to find those indices.我不太担心速度,因为我认为我的比较已经足够快了,特别是考虑到所涉及的数据量很小并且碰撞的相对罕见,但我仍然希望找到一个廉价的解决方案来找到这些索引。 I could create dictionaries, etc. I'm not sure what's the best route.我可以创建字典等。我不确定什么是最好的路线。 My test code for the comparison is below, and working great and gives me half of my answer, the indices of the bots making the collisions.我的比较测试代码如下,运行良好,给了我一半的答案,即产生碰撞的机器人的索引。 All I need now is the indices of the food collided with.我现在需要的只是与之碰撞的食物的索引。

from random import *
food = [(randint(0, 1000), randint(0, 1000)) for i in range(100)]
while True:
    bots = [(randint(0, 1000), randint(0, 1000)) for i in range(10)]
    for i in range(len(bots)):
        if bots[i] in food:
            print(i, bots[i])
    for i in range(len(food)):
        if food[i] in bots:
            print(i, food[i])

The above code does precisely what I want but is a dirty bodge and extremely expensive.上面的代码正是我想要的,但它很脏而且非常昂贵。 It gets me going for now but it will have to be refined.它让我现在可以继续前进,但必须对其进行改进。

It's hard to give a concrete answer because I don't know what a GA simulation is.很难给出具体的答案,因为我不知道 GA 模拟是什么。 Genetic Algorithm?遗传算法? Is it 2d, 3d, nd?是 2d,3d,nd? The approach for any of them will be similar.它们中的任何一个的方法都是相似的。

It's probably best to approach this a little differently.最好以不同的方式处理这个问题。

We have a "Location" that contains: it's identifier ie x,y(,z...) coordinates, a value for a bot to exist, and a value for food to exist.我们有一个“位置”,它包含:它的标识符,即 x,y(,z...) 坐标、机器人存在的值和食物存在的值。 Then the primary data structure is a list of all these locations.然后主要数据结构是所有这些位置的列表。

Now, for every frame (step/generation/etc), we iterate all possible locations and if the value exists for both food and a bot at any location, we know we have a collision.现在,对于每一帧(步骤/生成/等),我们迭代所有可能的位置,如果任何位置的食物和机器人的值都存在,我们知道我们有碰撞。 From there, it's just integrating whatever logic you want for your sim.从那里开始,它只是集成了您想要为您的 sim 提供的任何逻辑。

For food, use dictionary instead of list....对于食物,使用字典而不是列表....

from random import randint

food = {i:(randint(0, 1000), randint(0, 1000)) for i in range(100)}
while True:
    
    bots = [(randint(0, 1000), randint(0, 1000)) for i in range(10)]
    for i in range(len(bots)):
        if bots[i] in food.values():
            
            print(f'Bot num {i} collided with food num {list(food.keys())[list(food.values()).index(bots[i])]}')
            # "list(food.keys())[list(food.values()).index(bots[i])]" finds the key of the given value (here, the bot position)

Output: Output:

Bot num 3 collided with food num 80
Bot num 6 collided with food num 51
Bot num 3 collided with food num 32
Bot num 8 collided with food num 86
Bot num 7 collided with food num 98
Bot num 9 collided with food num 94
Bot num 5 collided with food num 88

This continues till I stop.这种情况一直持续到我停下来。

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

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