You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python2转Python3:defaultdict结合count迭代器报错求助

Got it, let's break this down and fix it for Python 3!

First, let's understand why your modified code throws that error:

fmap = defaultdict(next(count(1))) TypeError: first argument must be callable or None

When you write next(count(1)), you're immediately executing the next() call, which returns the integer 1—not a callable function/method. defaultdict requires its first argument to be something it can call every time it needs to generate a new default value, so passing a plain integer causes the type error.

Your original Python 2 code defaultdict(count(1).next) worked because it passed the .next() method of the counter iterator directly to defaultdict. Every time a new default value was needed, that method would be called to get the next incrementing number (1, 2, 3...).

The Fix for Python 3

You need to wrap the next() call in a reusable callable, while keeping the same counter instance so it increments properly. Here's the correct approach:

from collections import defaultdict
from itertools import count

# First create a persistent counter iterator
counter = count(1)
# Use a lambda to wrap next(counter) as a callable for defaultdict
fmap = defaultdict(lambda: next(counter))

How This Works

  • We first instantiate a single count(1) iterator so it maintains its state across calls.
  • The lambda acts as a callable wrapper: every time fmap encounters a new key, it runs next(counter) to get the next incrementing value.

Test it out to confirm:

print(fmap["x"])  # Outputs 1
print(fmap["y"])  # Outputs 2
print(fmap["z"])  # Outputs 3

⚠️ Don't make the mistake of writing defaultdict(lambda: next(count(1)))—that would create a new count(1) iterator every time, so you'd always get 1 as the default value, which isn't what the original code intended.

内容的提问来源于stack exchange,提问作者Natalie Shapira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:12:23