首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有将数字添加到列表中的数字中。

没有将数字添加到列表中的数字中。
EN

Stack Overflow用户
提问于 2017-04-21 03:06:42
回答 2查看 53关注 0票数 0

我正试着做一个进化模拟器,但我遇到了一些麻烦。我试图给列表中的数字添加一个“随机突变”,但它们实际上不会加在一起。

代码语言:javascript
复制
def Evolve(evolution, creatures):
    print(creatures)
    creatureNumber = 0
    if evolution == 0:
        print("First Generation:")
        for i in creatures:
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", i)
            randomMutation = random.randint(-1, 2)
            i += randomMutation
    else:
        print("Evolution", evolution, ":")
        for i in creatures:
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", i)
            randomMutation = random.randint(-1, 2)
            i += randomMutation
    print("")
    print("Leading Creature:", creatures.index(max(creatures))+ 1,":", max(creatures))
    EvolveQuestion(evolution, creatures)

“生物”列表是在一个单独的函数中定义的。"i += randomMutation“似乎根本没有把数字相加在一起。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-21 03:13:46

我已经指出,您已经忘记了creatureNumber + 1中的等号,所以您已经用修正的代码更新了您的问题,但是仍然没有得到预期的结果。好的。下面是您的代码的下一个主要问题:

如果您有一个类似于creatures = [1,1,1,1]的列表,并且您希望将其更改为[1,2,3,4],那么您必须按如下方式进行:

i的,_ in枚举(生物): creaturesi = i+1

现在我们回到编码。

很难从您提供的代码中猜出您想要实现什么,但我还是会偶然地尝试一下:

代码语言:javascript
复制
import random
creatures = [1,1,1,1]

def Evolve(evolution, creatures):
    print(creatures)
    creatureNumber = 0
    if evolution == 0:
        print("First Generation:")
        for i, item in enumerate(creatures):
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", creatures[i])
            randomMutation = random.randint(-1, 2)
            creatures[i] += randomMutation
    else:
        print("Evolution", evolution, ":")
        for i, item in enumerate(creatures):
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", creatures[i])
            randomMutation = random.randint(-1, 2)
            creatures[i] += randomMutation
    print("")
    print(creatures)    
    print("Leading Creature:", creatures.index(max(creatures))+ 1,":", max(creatures))
    print('---')
    # EvolveQuestion(evolution, creatures)

Evolve(0, creatures)
Evolve(1, creatures)
Evolve(2, creatures)
Evolve(3, creatures)
Evolve(4, creatures)
Evolve(5, creatures)

上面的代码是我对你打算实现什么的最好的猜测,但在某些方面它仍然没有任何意义,但至少它显示了一些“进化”。现在我很想看看你是否认为这对你有帮助。我会从你的评论或如果你接受我的回答:)注意到这一点。

代码显示为打印输出:

代码语言:javascript
复制
[1, 1, 1, 1]
First Generation:
                    Creature 1 : 1
                    Creature 2 : 1
                    Creature 3 : 1
                    Creature 4 : 1

[3, 0, 2, 3]
Leading Creature: 1 : 3
---
[3, 0, 2, 3]
Evolution 1 :
                    Creature 1 : 3
                    Creature 2 : 0
                    Creature 3 : 2
                    Creature 4 : 3

[3, 1, 4, 2]
Leading Creature: 3 : 4
---
[3, 1, 4, 2]
Evolution 2 :
                    Creature 1 : 3
                    Creature 2 : 1
                    Creature 3 : 4
                    Creature 4 : 2

[5, 3, 3, 2]
Leading Creature: 1 : 5
---
[5, 3, 3, 2]
Evolution 3 :
                    Creature 1 : 5
                    Creature 2 : 3
                    Creature 3 : 3
                    Creature 4 : 2

[6, 2, 3, 1]
Leading Creature: 1 : 6
---
[6, 2, 3, 1]
Evolution 4 :
                    Creature 1 : 6
                    Creature 2 : 2
                    Creature 3 : 3
                    Creature 4 : 1

[5, 3, 4, 0]
Leading Creature: 1 : 5
---
[5, 3, 4, 0]
Evolution 5 :
                    Creature 1 : 5
                    Creature 2 : 3
                    Creature 3 : 4
                    Creature 4 : 0

[4, 4, 6, 1]
Leading Creature: 3 : 6
---
票数 1
EN

Stack Overflow用户

发布于 2017-04-21 03:18:20

Look here first。不能修改迭代变量i += randomMutation。它将被creatures上的迭代器返回的下一个元素覆盖。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43533265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档