0

I have a dictionary dico that i created it with this code :

dico = {}
for index, row in data.iterrows():
    
    tup = (row['OsId'], row['BrowserId'])
    
   
    if tup not in dico:
        dico[tup] = []
    dico[tup].append(row['PageId'])
[print(f"{key} : {value}") for key, value in dico.items()]

here a sample of dico :

 combination : list of pages :

(99, 14) : [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903]
(33, 16) : [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761]
(12, 99) : [1068379, 1184903, 955764, 955761, 1184903, 955764]
(11, 99) : [1187774]

I am looking for a way to change the dico to replace the combination value by it's index in the list of combinations

For example i have the list of combination : (99, 14), (33, 16), (12, 99), (11, 99)

The expected result should be :

0 : [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903]
1 : [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761]
2 : [1068379, 1184903, 955764, 955761, 1184903, 955764]
3 : [1187774]

Any idea please to do it? thanks

user17241
  • 307
  • 1
  • 4
  • 16

6 Answers6

2

With a list of keys key_list = [(99, 14), (33, 16), (12, 99), (11, 99)]:

dict(enumerate(dico[k] for k in key_list))
fsimonjetz
  • 5,644
  • 3
  • 5
  • 21
0

Is this what you want:

{i: value for i, value in enumerate(dico.values())}

0

You cannot rename keys. You can iterate the list of keys (not directly) and insert the value of key old under another key and delete the old one:

keys = (99, 14), (33, 16), (12, 99), (11, 99)

d = {}
# iterates the LIST of keys, not dict.keys - that would not work
for idx, k in enumerate(keys,1):
    d[k] = 1111*idx

# before
print(d)

for idx,old in enumerate(keys):
    d[idx] = d[old]    # copy value
    del d[old]         # delete old key from dict

# after
print(d)

Output before:

{(99, 14): 1111, 
 (33, 16): 2222, 
 (12, 99): 3333, 
 (11, 99): 4444}

Output after:

{0: 1111, 
 1: 2222, 
 2: 3333, 
 3: 4444}

Or you create a fully new dict from it.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

This is a possible solution:

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

However, keep in mind that you're creating a new dictionary from scratch.

Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
0

The simplest solution is:

dico = dict(enumerate(dico.values()))

enumerate(dico.values()) gives you the equivalent of zip(range(len(dico)), dico.values()). Passing that sequence of tuples to dict() creates a new dictionary that uses the first element of each tuple as the key and the second element as the value.

Samwise
  • 68,105
  • 3
  • 30
  • 44
0

replace the combination value by it's index in the list of combinations

what about if there is no such combination in the list? maybe something like this?:

comb_list = [(99, 14), (33, 16), (12, 99), (11, 99)]
dico = {(99, 14) : [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903],
        (33, 16) : [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761],
        (12, 99) : [1068379, 1184903, 955764, 955761, 1184903, 955764],
        (11, 99) : [1187774],
        (77, 77) : [7777777]}  # not in the list

dico = {comb_list.index(k) if k in comb_list else k:v for k,v in dico.items()}
print(dico)
'''
{0: [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903],
 1: [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761],
 2: [1068379, 1184903, 955764, 955761, 1184903, 955764],
 3: [1187774],
 (77, 77): [7777777]}
SergFSM
  • 1,419
  • 1
  • 4
  • 7