2

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

user17241
  • 307
  • 1
  • 4
  • 16
  • for each key read the `Values` as an array. Now for each element in Values Array, subtract ASCII value of character 'A' from the ASCII value of current element. Store this value back to the array. Reassign this array to the key. – Arpit Chinmay Mar 06 '22 at 18:04
  • 1
    Would you mind sharing what you have tried so far? – TheFaultInOurStars Mar 06 '22 at 18:04

2 Answers2

4

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]}
catasaurus
  • 933
  • 4
  • 20
0

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'])
SergFSM
  • 1,419
  • 1
  • 4
  • 7