字典到Python中的小写字母

编程入门 行业动态 更新时间:2024-10-27 06:33:20
本文介绍了字典到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中的小写字母

本文发布于:2023-10-16 17:03:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   字母   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!