有效的方法来删除字符串中的空字符串的键 [英] Efficient way to remove keys with empty strings from a dict
本文介绍了有效的方法来删除字符串中的空字符串的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
metadata = { u'Composite:PreviewImage':u'(二进制数据101973字节)',
u'EXIF:CFAPattern2':u''
最好的方法是什么?
解决方案
((k,v)for k,v in metadata.iteritems()if v)
请注意,值。只是这些值中的一些是空字符串。没有价值的,没有一个关键字的东西,如果它没有价值,那就不会在dict里。
I have a dict and would like to remove all the keys for which there are empty value strings.
metadata = {u'Composite:PreviewImage': u'(Binary data 101973 bytes)',
u'EXIF:CFAPattern2': u''}
What is the best way to do this?
解决方案
dict((k, v) for k, v in metadata.iteritems() if v)
Note that all of your keys have values. It's just that some of those values are the empty string. There's no such thing as a key in a dict without a value; if it didn't have a value, it wouldn't be in the dict.
这篇关于有效的方法来删除字符串中的空字符串的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文