本文介绍了字典到Python中的小写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
除了字典,我想这样做:
I wish to do this but for a dictionary:
"My string".lower()有内置函数还是应该使用循环?
Is there a built in function or should I use a loop?
推荐答案您将需要使用循环或列表/生成器理解.如果要小写所有键和值,可以执行以下操作:
You will need to use either a loop or a list/generator comprehension. If you want to lowercase all the keys and values, you can do this::
dict((k.lower(), v.lower()) for k,v in {'My Key':'My Value'}.iteritems())如果只想小写按键,可以执行以下操作:
If you want to lowercase just the keys, you can do this::
dict((k.lower(), v) for k,v in {'My Key':'My Value'}.iteritems())生成器表达式(上面使用)通常在构建字典时很有用;我一直都在用它们.循环理解的所有表现力,没有任何内存开销.
Generator expressions (used above) are often useful in building dictionaries; I use them all the time. All the expressivity of a loop comprehension with none of the memory overhead.
更多推荐
字典到Python中的小写字母
发布评论