dict的len的意外行为

2024-06-28 20:15:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我期望len(邮件)返回100,但我得到101。有人能解释我为什么会有这种行为吗

items_by_post = {}

for i in joined:
    if i['post_code'] in items_by_post:
        items_by_post[i['post_code']].append(i)
    elif len(items_by_post)<=100 and i['post_code'] not in items_by_post:
        items_by_post[i['post_code']]=items_by_post.get(i['post_code'],[])
        items_by_post[i['post_code']].append(i)
    else:
        continue

Tags: andinforgetbylenif邮件