Home > Back-end >  change value dictionary by index
change value dictionary by index

Time:03-07

I have a dictionary dico like this :

id_OS (keys)  :  List of pages(values)
0 :      [A, B]
1 :      [C, D, E]
2 :      [F, B]
3 :      [G, A, B]

I would like to change it to this form

 id_OS  :  List of index id_pages 

0 :      [0, 1]
1 :      [2, 3, 4]
2 :      [5, 1]
3 :      [6, 0, 1]

I try this code, but i didnt got the correct index of values :

dico = dict(zip(range(len(dico)), zip(range(len(dico.values())))))

Any idea please to do it

Thanks

CodePudding user response:

This should work:

letters = {0: ['A', 'B'], 1: ['C', 'Z']}
for key in letters:
    new_list = []
    for i in letters[key]:
        i = i.lower()
        new_list.append(ord(i) - 97)
    letters[key] = new_list

I subtracted 97 instead of 96 (The reason why 96 is subtracted is well explained in this post: Convert alphabet letters to number in Python) because it seems like you want everything to be shifted so that A is 0 not 1 like it would usually be.

Output:

{0: [0, 1], 1: [2, 25]}

CodePudding user response:

through your previous question I see that you could simplefy your task. I would change data['PageId'] type into categories and passed categories codes to the dictionary. Something like this:

data['codes'] = data['PageId'].astype('category').cat.codes

then change this line in your code:

dico[tup].append(row['PageId'])

into this:

dico[tup].append(row['codes'])
  • Related