Python automatically changes a variable from list
to str
inside a for
loop so I got an error when I append to the list
. Why does this happen?
By looking at your provided question and the output, I think that you are basically trying to swap the dictionary values of the given one.
So, the code for that is :
def func(dct):
new_dct = {}
for key, values in dct.items():
for value in values:
if value in new_dct:
new_dct[value].append(key)
else:
new_dct[value]=[key]
return (new_dct)
dct = {'local': ['admin', 'userA'],
'public': ['admin', 'userB'],
'administrator': ['admin']}
print(func(dct))
I guess this should work for you! Considering Upvoting if found helpful!
只需将第 14 行更改为ug = [groups,]
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.