列表& Python中的对象重复问题 [英] List & Object Duplication Issues in Python

查看:144
本文介绍了列表& Python中的对象重复问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前提过一个问题,涉及到循环和列表,并收到了一些很好的反馈。不幸的是,我遇到了一个新问题,我似乎无法自己解决。对于大块代码,我的歉意:

 导入随机
从pprint import pprint

petri_dish = []
lst = [y for petri_dish if y.status == 1]

turn = 1

class种类:
#__ init __,relocate,fight和target methods

for range(20):
petri_dish.append(Species(n,0,0,0,0,1))

def再生():
类子代(物种):
通过
为列表中的z(petri_dish):
如果z.status == 1和z.life> = 200:
petri_dish.append(后代('A'+ str(z.name),0,0,0,0,1))

def move_around():
for list(petri_dish):
如果转%2000 == 0:
reproduction()
x.relocate()
x .target()

while len([y for y in petri_dish if y.status == 1])> 1:
turn + = 1
move_around()

for x在[y for petri_dish if y.status == 1]:
pprint(vars (x))

打印转

想法是复制最强的细胞每转一圈。问题是这些单元被复制太多次了;如果你运行代码几次,你一定会看到我所指的是什么。



我的怀疑是,我正在尝试更改一个列表,我正在迭代,或者我以某种方式错误地引用了某个列表,但我无法确定问题点。



任何和所有的帮助将不胜感激。谢谢!

解决方案

我知道这不是OP原来寻找的答案,但可能是正确的答案,如果OP找到问题,那么也许他们会认为这是正确的答案。



尝试 debug 具有断点的代码。为了方便使用,没有什么节拍 IPython pdb ,尽管 pdb - Python调试器 winpdb 也是有用的。如果您使用的是 Spyder PyDev 插件,用于 eclipse 调试内置在图形用户界面中 - 只是设置你的断点并去。



对于你的代码:


  1. 安装Ipdb和它的依赖关系(IPython,& c。)

  2. 从系统命令行可以使用方便的 ipdb 脚本。

     〜$ ipdb --help 

    用法:ipdb.py scriptfile [arg]
    〜$ ipdb species.py args
    > 〜\species.py(1)< module>()
    ---> 1进口随机
    2从pprint import pprint
    3

    ipdb>


  3. 命令与 pdb 和任何调试器。在 ipdb> 提示之后输入帮助,以获取命令列表。



    基本命令是




    • n for next 执行当前行并转到下一个,

    • s 步骤执行被调用对象的下一行,例如函数或

    • r to 返回给呼叫者,

    • b 设置断点

    • c 继续执行,直到下一个断点和

    • q 退出退出



    通过键入 help< cmd> 。 EG

      ipdb>帮助r 
    r(eturn)
    继续执行,直到当前函数返回。


  4. 在您的代码中设置一个断点,您认为这是一个麻烦,可以通过它来实现。

      ipdb> b 67 
    断点1在〜\species.py:67


  5. 你可以使用Python命令并检查几个限制的变量 - retval rv 和任何 ipdb 命令将返回 ipdb 调用的结果 - 所以使用 vars()['< varname>']


  6. ipdb 中的列表推理就像for循环,因此, n 将逐步浏览列表的理解次数与迭代的长度一样多。


  7. 点击enter / return键重复最后一个 ipdb 命令。 EG

      ipdb> n 
    > 〜\species.py(67)< module>()
    66
    1 - > 67 while len([y for y in petri_dish if y.status == 1])> 1:
    68 turn + = 1

    ipdb>
    > 〜\species.py(67)< module>()
    66
    1 - > 67 while len([y for y in petri_dish if y.status == 1])> 1:
    68 turn + = 1

    ipdb>
    > 〜\species.py(69)< module>()
    68 turn + = 1
    ---> 69 move_around()
    70

    ipdb>转
    2


  8. 进入一个函数看看它的作用

      ipdb> s 
    --Call -
    > 〜\species.py(60)move_around()
    59
    ---> 60 def move_around():
    61 for x in list(petri_dish):


希望你得到想法。学习使用调试器将会有更多的回报比有别人发现你的错误。至少,如果您可以确定出现多余的重复项目的出现位置,那么您可以询问一个更具体的问题,您会得到更好的答案。



快乐编码! / p>

I had asked a question earlier that involved loops and lists and received some great feedback. Unfortunately, I've run into a new issue that I just can't seem to solve by myself. My apologies for the large block of code:

import random
from pprint import pprint

petri_dish = []
lst = [y for y in petri_dish if y.status == 1]

turn = 1

class Species:
    #__init__,relocate, fight, and target methods

for n in range(20):
    petri_dish.append(Species(n,0,0,0,0,1))

def reproduce():
    class Offspring(Species):
        pass
    for z in list(petri_dish):
        if z.status == 1 and z.life >= 200:
            petri_dish.append(Offspring('A'+str(z.name),0,0,0,0,1))

def move_around():
    for x in list(petri_dish):
        if turn % 2000 == 0:
            reproduce()
        x.relocate()
        x.target()

while len([y for y in petri_dish if y.status == 1]) > 1:
    turn += 1     
    move_around()

for x in [y for y in petri_dish if y.status == 1]:
    pprint(vars(x))

print turn

The idea is to duplicate the "strongest" cells every certain number of turns. The problem is that these cells are being copied too many times; if you run the code a few times, you're bound to see what I'm referring too.

My suspicion is that I'm trying to change a list that I'm iterating over or that I'm somehow incorrectly referencing a list somewhere, but I can't pinpoint the problem spot.

Any and all help would be greatly appreciated. Thank you!

解决方案

I know this isn't exactly the answer the OP was looking originally looking for, but it might be the correct answer, and if the OP manages to find the issue, then maybe they will think it is the correct answer too.

Try to debug the code with breakpoints. For ease of use nothing beats IPython pdb, although pdb - Python debugger, and winpdb are also useful. If you are using Spyder or PyDev plugin for eclipse debugging is built into the graphical user interface - just set your breakpoints and go.

For your code:

  1. Install Ipdb and its dependencies (IPython, &c.)
  2. From the system command line you can use the handy ipdb script.

    ~ $ ipdb --help
    
    usage: ipdb.py scriptfile [arg] ...
    ~ $ ipdb species.py args
    > ~\species.py(1)<module>()
    ---> 1 import random
         2 from pprint import pprint
         3
    
    ipdb>
    

  3. Commands are the same as pdb and for any debugger. Type help after the ipdb> prompt for a list of commands.

    The basic commands are

    • n for next to execute the current line and step to the next,
    • s or step to execute the next line of a called object, such as a function or class constructor or method,
    • r to return to the caller,
    • b to set a breakpoint
    • c to continue execution until the next breakpoint and
    • q to quit or exit

    Get more help by typing help <cmd>. EG

    ipdb> help r
    r(eturn)
    Continue execution until the current function returns.
    

  4. Set a breakpoint in your code where you think the trouble might be and step through it.

    ipdb> b 67
    Breakpoint 1 at ~\species.py:67
    

  5. You can use Python commands and examine variables with few restrictions - retval, rv and any of the ipdb commands will return the result of that ipdb call - so use vars()['<varname>'] instead.

  6. List comprehensions in ipdb are like for-loops, and so n will step through the list comprehension as many times as the length of the iterable.

  7. Hit enter/return key to repeat the last ipdb command. EG

    ipdb> n
    > ~\species.py(67)<module>()
         66
    1--> 67 while len([y for y in petri_dish if y.status == 1]) > 1:
         68     turn += 1
    
    ipdb>
    > ~\species.py(67)<module>()
         66
    1--> 67 while len([y for y in petri_dish if y.status == 1]) > 1:
         68     turn += 1
    
    ipdb>
    > ~\species.py(69)<module>()
         68     turn += 1
    ---> 69     move_around()
         70
    
    ipdb> turn
    2
    

  8. Step into a function to see what it does

    ipdb> s
    --Call--
    > ~\species.py(60)move_around()
         59
    ---> 60 def move_around():
         61     for x in list(petri_dish):
    

Hopefully you get the idea. Learning to use a debugger is going to have more payback than having someone else spot your bug. At the very least, if you can pinpoint where the extra duplicates start appear, then you can ask a more specific question and you will get better answers.

Happy coding!

这篇关于列表&amp; Python中的对象重复问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆