top | item 27053735

(no title)

scienceman | 4 years ago

Most "straightforward" way I could think of:

```

import itertools, collections

cnt = itertools.count(1)

d = collections.defaultdict(lambda: next(cnt))

s = "abcad"

[d[c] for c in s]

```

discuss

order

klyrs|4 years ago

Oh, this is nice. You can do it in one line (plus imports) with

  d = defaultdict(count().__next__)

scienceman|4 years ago

Interesting... I wonder if this is vulnerable to getting garbaged collected at any point during execution.