I've got a dictionary like
dic ={('L', 'N', 'C'):6, ('N', 'L', 'C'):4, ('C', 'N', 'L'):3})
I want to remove the string 'C' from all keys.
Is there any efficient way of doing so
解决方案
This can be done with a single dictionary comprehension:
>>> dic ={('L', 'N', 'C'):6, ('N', 'L', 'C'):4, ('C', 'N', 'L'):3}
>>> {tuple(l for l in k if l != 'C'):v for k,v in dic.items()}
{('L', 'N'): 6, ('N', 'L'): 4}
Note that the removal of 'C' makes ('N', 'L', 'C') and ('C', 'N', 'L') clash as they both become ('N', 'L'). It is not clear from the question how you wish to handle that.