Python字典:从元组中删除一个字符串(WHICH是一个关键字。) [英] Python Dictionary:removing a string from a tuple( WHICH is a key .)

查看:522
本文介绍了Python字典:从元组中删除一个字符串(WHICH是一个关键字。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典,如

dic ={('L', 'N', 'C'):6, ('N', 'L', 'C'):4, ('C', 'N', 'L'):3})

我想从所有键中删除字符串C。
有没有任何有效的方法这样做

I want to remove the string 'C' from all keys. Is there any efficient way of doing so

推荐答案

这可以通过一个字典理解

>>> 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}

请注意,删除'C '('N','L','C')('C','N' ,'L')冲突,因为它们都成为('N','L')。从你想如何处理这个问题的问题不清楚。

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.

这篇关于Python字典:从元组中删除一个字符串(WHICH是一个关键字。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆