Dr. Big Man
Dr. Big Man

Reputation: 31

How do I automatically increment value for definition of constant list of indices in Python?

This is my first question on stackoverflow and I am a complete newbie to Python - please bear with me. Definitely tried a lot to find solution on the internet - probably did not use correct search terms?

In order to symbolically address entries in an array, I would like to define a list of indices, e.g.

USRNM_IDX = 0
TIMSTP_IDX = 1
MSISDN_IDX = 2

Sometimes, I might need to apply changes to the list: reorder, insert new or remove existing values:

USRNM_IDX = 0
AMNT_IDX = 1
TIMSTP_IDX = 2
MSISDN_IDX = 3

and will need to manually change indices to remain correct - tedious and error-prone. There will most definitely be a better way to do that. The best I could come up with until now:

run_idx = 0
USRNM_IDX = run_idx; run_idx += 1 
TIMSTP_IDX = run_idx; run_idx += 1       
MSISDN_IDX = run_idx; run_idx += 1       
AMNT_IDX = run_idx; run_idx += 1

Not exactly beautiful, I would say. Maybe there's a completely different idiom in Python for this, that I am not yet aware of? Thank you in advance for your help.


Many thanks already - guess you are right about the XY problem. Here's the "larger question".

The Python program will process some log file and store information in a dictionary. Each entry in the dictionary will itself be an array. Now I want to index that array 'symbolically', e.g.

trkey_dict[trkey][MSISDN_IDX] = this_msisdn
trkey_dict[trkey][CARRIER_IDX] = this_carrier
trkey_dict[trkey][TIMSTP_IDX] = this_timestamp

If I learned correctly, arrays can only be addressed by integers - so I am looking for a smart way to create some variables (could be constant - but alas...) with meaningful names in order to not use literal values, when accessing array elements. This will also allow me, to rearrange layout of the array very easily in order to finally output in the correct order for my result list, e.g.:

for trkey in trkey_dict.keys(): print trkey,";"," ; ".join(trkey_dict[trkey])

Currently I am trying to better understand all the other aggregated data types, that Python provides: tuples, sets etc. - not sure, if some of those might provide better solution.


Answer given by DonCallisto would make creation of the list of indices really nice. I wouldn't even need to use insert, as the list of indices will not be changed dynamically. So

 mylist = ['USRNM_IDX', 'TIMSTP_IDX', 'MSISDN_IDX']

would really do nicely. However, addressing array elements would then always require use of

mylist.index('USRNM_IDX')

and thus result e.g. in

trkey_dict[trkey][mylist.index('MSISDN_IDX')] = this_msisdn
trkey_dict[trkey][mylist.index('CARRIER_IDX')] = this_carrier
trkey_dict[trkey][mylist.index('TIMSTP_IDX')] = this_timestamp

which doesn't look really pretty to me then again.

Many thanks to everybody sharing comments and answer. Will update, as I learn more.

Upvotes: 2

Views: 565

Answers (1)

DonCallisto
DonCallisto

Reputation: 29912

Why don't you use a list and use list-based operation?

Like

mylist = ['USRNM_IDX', 'TIMSTP_IDX', 'MSISDN_IDX']
mylist.insert(1, 'AMNT_IDX')

If you need the value (so the index) you could use

mylist.index('TIMSTP_IDX')

Upvotes: 2

Related Questions