Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign strings to IDs in Python

Tags:

python

string

I am reading a text file with python, formatted where the values in each column may be numeric or strings.

When those values are strings, I need to assign a unique ID of that string (unique across all the strings under the same column; the same ID must be assigned if the same string appears elsewhere under the same column).

What would be an efficient way to do it?

like image 818
Bob Avatar asked Dec 09 '22 13:12

Bob


1 Answers

Use a defaultdict with a default value factory that generates new ids:

ids = collections.defaultdict(itertools.count().next)
ids['a']  # 0
ids['b']  # 1
ids['a']  # 0

When you look up a key in a defaultdict, if it's not already present, the defaultdict calls a user-provided default value factory to get the value and stores it before returning it.

collections.count() creates an iterator that counts up from 0, so collections.count().next is a bound method that produces a new integer whenever you call it.

Combined, these tools produce a dict that returns a new integer whenever you look up something you've never looked up before.

like image 174
user2357112 supports Monica Avatar answered Jan 06 '23 06:01

user2357112 supports Monica