【问题标题】:Python3 getting list item index returning empty errorPython3获取列表项索引返回空错误
【发布时间】:2017-06-29 15:25:08
【问题描述】:

代码:

            other_users=User.objects.filter(logged_in=False)

            for other_user in other_users:
                other_posts=Post.objects.filter(author=other_user)
                print('[!] Collecting other_posts from other_user => {} [{}] [!]'.format(other_user,other_posts))
                try:
                    for other_post in other_posts:
                        print('[=] other_post: {}'.format(other_post))
                        if other_post.likes==0:
                            print('[=] other_post with 0 likes: {}'.format(other_post))
                            for like in range(max_likes_per_user):
                                rand_post=random.choice(other_posts)
                                print('[+] Random post => {} : TYPE[{}]'.format(rand_post,type(rand_post)))
                                post_index=other_posts.index(rand_post)
                                print('[!] Post index: {}'.format(post_index))
                                other_posts.pop(post_index)
                                print('[+] Liking {} POST'.format(rand_post))
                                rand_post.likes+=1
                                rand_post.save()
                                print('[!] Saving like [!]')
                except Exception as e:
                    print('[-] Error: [-]\n\n'.format(e))
                    done=True

我试图找出过去 1 小时的问题出在哪里,但仍然找不到。

我正在尝试从列表中获取随机选择的索引,但每当我到达该部分时,程序就会以空错误退出。

部分:

post_index=other_posts.index(rand_post)
print('[!] Post index: {}'.format(post_index))

之后的任何内容都不会返回任何内容,而是直接跳向异常,但是,e 变量始终为空,我没有得到错误输出。

我想知道为什么 post_index 变量没有得到任何数据,为什么它会跳到异常部分。代码中有什么不好的地方?

这些是我得到的输出:

[!] Collecting other_posts from other_user => Robot Tyler 1 [<QuerySet [<Post: Robot Tyler 1 - Title 1>, <Post: Robot Tyler 1 - Title 2>, <Post: Robot Tyler 1 - Title 3>, <Post: Robot Tyler 1 - Title 4>]>] [!]
[=] other_post: Robot Tyler 1 - Title 1
[=] other_post with 0 likes: Robot Tyler 1 - Title 1
[+] Random post => Robot Tyler 1 - Title 4 : TYPE[<class 'person.models.Post'>]
[-] Error: [-]


[!] Collecting other_posts from other_user => Robot Tyler 2 [<QuerySet [<Post: Robot Tyler 2 - Title 1>, <Post: Robot Tyler 2 - Title 2>, <Post: Robot Tyler 2 - Title 3>]>] [!]
[=] other_post: Robot Tyler 2 - Title 1
[=] other_post with 0 likes: Robot Tyler 2 - Title 1
[+] Random post => Robot Tyler 2 - Title 1 : TYPE[<class 'person.models.Post'>]
[-] Error: [-]

谁能告诉我错在哪里?

【问题讨论】:

  • 这不是真正的提问方式……问清楚
  • 哪部分代码有问题?
  • 您能否发布错误的完整堆栈跟踪。这样会更清楚。
  • 如果您在异常打印语句中添加 '{}' 会有所帮助,或者直截了当地说 - print(e) - 因此实际打印了异常。我的猜测是您的数据不支持对索引的这种引用
  • 您可以删除隐藏错误的异常处理程序:这将显示回溯!!

标签: list python-3.x indexing


【解决方案1】:

由于某种原因,other_posts 变量是 QuerySet 而不是列表。所以我所做的就是将其转换为list(other_posts)。如果有人似乎理解 other_posts 被寻址为 QuerySet 背后的原因,请在 cmets 中解释。

这就是代码现在的样子:

other_users=User.objects.filter(logged_in=False)

for other_user in other_users:
    other_posts=Post.objects.filter(author=other_user)
    print('[!] Collecting other_posts from other_user => {} [{}] [!]'.format(other_user,other_posts))
    try:
        for other_post in other_posts:
            print('[=] other_post: {}'.format(other_post))
            if other_post.likes==0:
                print('[=] other_post with 0 likes: {}'.format(other_post))
                for like in range(max_likes_per_user):
                    rand_post=random.choice(other_posts)
                    print('[+] Random post => {} : TYPE[{}]'.format(rand_post,type(rand_post)))
                    post_index=list(other_posts).index(rand_post)
                    print('[!] Post index: {}'.format(post_index))
                    list(other_posts).pop(post_index)
                    print('[+] Liking {} POST'.format(rand_post))
                    rand_post.likes+=1
                    rand_post.save()
                    print('[!] Saving like [!]')
    except Exception as e:
        print(e)
        done=True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-15
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    相关资源
    最近更新 更多