Re: [Tutor] Dictionary viceversa

2018-07-30 Thread Alan Gauld via Tutor
On 30/07/18 19:11, Zachary Ware wrote: > On Mon, Jul 30, 2018 at 1:08 PM Alan Gauld via Tutor wrote: >> There are lots of options including those suggested elsewhere. >> Another involves using get() which makes your function >> look like: >> >> def viceversa(d): >> new_d = dict() >> for k

Re: [Tutor] Dictionary viceversa

2018-07-30 Thread Zachary Ware
On Mon, Jul 30, 2018 at 1:08 PM Alan Gauld via Tutor wrote: > There are lots of options including those suggested elsewhere. > Another involves using get() which makes your function > look like: > > def viceversa(d): > new_d = dict() > for k in d: > for e in d[k]: > new

Re: [Tutor] Dictionary viceversa

2018-07-30 Thread Alan Gauld via Tutor
On 30/07/18 13:40, Valerio Pachera wrote: > users = {'user1':['office-a', 'office-b'], > 'user2':['office-b'], > 'user3':['office-a','office-c']} > > It's a list of users. > For each user there's a list of room it can access to. > > I wish to get the same info but "sorted" by room. Re

Re: [Tutor] Dictionary viceversa

2018-07-30 Thread Peter Otten
Zachary Ware wrote: > On Mon, Jul 30, 2018 at 12:20 PM Valerio Pachera wrote: >> I was looking to substiture the cicle for e in new_d like this: >> [ new_d[e].append(k) if e in new_d else new_d[e].append(k) for e in >> [ d[k] ] >> but it can't work because 'new_d[e] = []' is missing. > > Hav

Re: [Tutor] Dictionary viceversa

2018-07-30 Thread Zachary Ware
On Mon, Jul 30, 2018 at 12:20 PM Valerio Pachera wrote: > I was looking to substiture the cicle for e in new_d like this: > [ new_d[e].append(k) if e in new_d else new_d[e].append(k) for e in d[k] ] > but it can't work because 'new_d[e] = []' is missing. Have a look at `dict.setdefault` and pla

[Tutor] Dictionary viceversa

2018-07-30 Thread Valerio Pachera
Hi all, consider this dictionary users = {'user1':['office-a', 'office-b'], 'user2':['office-b'], 'user3':['office-a','office-c']} It's a list of users. For each user there's a list of room it can access to. Generalizing, a dictionary with a list for each element. d = {k:list} I wi