0

I want to emulate something like an enum, but slightly different. There are 'N' different entities within a list. The 'N' changes depending on the data. I want to assign each one of those 'N' entities a value from 1 to N. For certain entities, I want to give it them the same value.

For example, things = ['one', 'two', 'three', 'first', 'five']

I want to assign:

one = 1
two = 2
three = 3
first = 1
five = 5

How do I do this in a graceful manner?

user3666471
  • 907
  • 11
  • 24
  • If you are using python 3.4+ there is a new [enum module](https://docs.python.org/3/library/enum.html) – GWW Aug 15 '14 at 03:08
  • If you want to make many of these objects and on the fly then it sounds like you want an [OrderedDict](https://docs.python.org/2/library/collections.html#collections.OrderedDict). – Dunes Aug 15 '14 at 07:35

3 Answers3

0

Do you mean a dict?

things = ['one', 'two', 'three', 'first', 'five']
result = {}
for index, thing in enumerate(things, start=1):
    result[thing] = index
result['first'] = 1
print result

And then result is

{'three': 3, 'five': 5, 'two': 2, 'first': 1, 'one': 1}
lk_vc
  • 1,136
  • 20
  • 26
  • That's what I want to do, but I don't want to hard code it into my program. It needs to be something that dynamically adapts. – user3666471 Aug 15 '14 at 03:26
  • @user3666471, how do you get the mapping between the two? You did not provide that information. You can dynamically generate a dictionary. – Andy Aug 15 '14 at 03:57
  • I want the mapping to follow order, like if I give it a file which contains the order ['a', 'c', 'd', ....], it should map {'a':1, 'c':2, 'd':3, ...}. On certain elements, I would like it to break the pattern. – user3666471 Aug 15 '14 at 04:34
0

From How can I represent an 'Enum' in Python?

The way I'd go about it is as follows. Just define the typical enumeration using args and then put any of your special parameters in the keyword arguments.

def enum(*args, **kwargs):
    enums = dict(zip(args, range(len(args))), **kwargs)
    return type('Enum', (), enums)
test = enum('a','b','c',first = 1, second = 2)
print test.a
print test.b
print test.c
print test.first
print test.second

Yields:

0
1
2
1
2

Also, this will use 0 based indexing. If you want a base of 1, as in your example, use

range(1,len(args)+1))

instead of

range(len(args))

To me, it seems like quite a hassle if you have to skip values (like four) and have specially assigned values (like first) thrown in randomly. In that case, I don't think you could use this solution (or something like it). Instead, you'd probably have to find any specially assigned strings and give those values, which is going to be a lot less graceful.

Community
  • 1
  • 1
Gustav
  • 688
  • 5
  • 12
0

Use the Python function enumerate. That is what it is there for. As in

 enumerate(things)

You can convert it to whatever you want. Like:

dict(enumerate(things)) # =  {0: 'one', 1: 'two', 2: 'three', 3: 'first', 4: 'five'}
list(enumerate(things)) # = [(0, 'one'), (1, 'two'), (2, 'three'), (3, 'first'), (4, 'five')]

etc ...

The Pythonic way of going it is to create a generator by just using enumerate where you need it. This avoids generating extra data and consuming memory, especially when your original list is very long.

ssm
  • 5,277
  • 1
  • 24
  • 42