zoukankan      html  css  js  c++  java
  • 集体编程智慧(发现的一些代码问题)

    如果你在看集体编程智慧这本书,第五章的遗传算法时候,出现这样的错误:TypeError: object of type 'NoneType' has no len()。。

    请注意下面这个函数:

     

      def mutate(vec):
        i=random.randint(0,len(domain)-1)
        if random.random()<0.5 and vec[i]>domain[i][0]:
          return vec[0:i]+[vec[i]-step]+vec[i+1:] 
        elif vec[i]<domain[i][1]:
          return vec[0:i]+[vec[i]+step]+vec[i+1:]

     

    若输入vec同时不满足两个if的条件,该函数就不会有return 语句。因此该函数就会默认返回None.所以就出现了上面的错误。

     

    修改方法如下,在函数最后一句加上return vec,或者其他方法,总之要返回一个列表

     

      def mutate(vec):
        i=random.randint(0,len(domain)-1)
        if random.random()<0.5 and vec[i]>domain[i][0]:
          return vec[0:i]+[vec[i]-step]+vec[i+1:] 
        elif vec[i]<domain[i][1]:
          return vec[0:i]+[vec[i]+step]+vec[i+1:]
        return vec

    第六章,如果你的一些概率计算为0,可能是int除法的一些问题,即整数2/3=0,在相应变量前面乘以1。0就可以了。

  • 相关阅读:
    kali更新源
    中国Linux源镜像站大全
    火狐浏览器Firefox上DownThemAll插件
    使用 backdoor 工具注入ShellCode
    动态加载 ShellCode绕过杀软
    渗透测试的本质与沉思
    Payload 实现分离免杀
    XSS跨站攻击靶场-通关笔记
    Web文件上传靶场
    Python 常用外部模块详解
  • 原文地址:https://www.cnblogs.com/Dzhouqi/p/3203766.html
Copyright © 2011-2022 走看看