【问题标题】:Boolean evaluation between two lists is not working两个列表之间的布尔评估不起作用
【发布时间】:2013-09-11 21:24:37
【问题描述】:

我对这个评估有疑问,“旧”,它是一个元组列表,当使用 bp_list 评估时应该评估 False,bp_list 是相同的元组列表,但少了一个项目。非常感谢您。

while True: # game loop
    if dead_flag == True:
        pygame.time.wait(2000)
        dead_flag = False    
    MAINSURF.fill(BLACK)

    thePill.drawPills(p_inventary)
    oldpoints = points
    p_inventary, points = thePill.eatenPill(pac._x, pac._y, p_inventary)

    bigPill(*bp_list)
    #### This is the problematic part
    old = bp_list
    print ">>>", old
    bp_list = eatBig(bp_list, pac._x, pac._y)
    print "---",bp_list
    if (old != bp_list):
        print "they're different"
        print "##", old_bp_list
        print "##", bp_list
        score += 50

    if oldpoints != points:
        score = (basep - points) * 10


    """else: 
        print "old_bp_list ", old_bp_list
        print "bp_list ", bp_list    """     
    if p_inventary == []:
        break

【问题讨论】:

  • 实际发生了什么?你得到什么输出?你调用的所有其他函数是做什么的?
  • 您能否删除代码并简化功能直到问题消失,然后仔细查看最后一个证明问题的版本?
  • 您是否希望old = bp_list 将old 变成bp_list 的副本?它没有。在该行之后,old 和 bp_list 都指向同一个对象,对 bp_list 的任何修改都将显示在 old 上。
  • 感谢用户 2357112!你是绝对正确的。我只是通过这样做将 bp_list 复制到一个新的参考资料中(我不知道我的条款是否正确):old = bp_list[:] 和瞧。你解决了我的问题,谢谢。如何将您的答案标记为有帮助?
  • 既然原来是这个问题,我把评论变成了你可以接受的答案。您应该会在它旁边看到一个复选标记的轮廓。

标签: python list boolean


【解决方案1】:
old = bp_list

此行不会将old 转换为bp_list 的副本。在这行之后,old 和 bp_list 都指向同一个对象,对 bp_list 的任何修改都会显示在 old 上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    相关资源
    最近更新 更多