更改大小写

时间:2018-12-21 11:40:10

标签: python list dictionary lowercase

我是python的新手,正在尝试将许多字符串的大小写更改为小写。

许多字符串作为列表的元素包含在内,列表本身是字典的键(字符串数据类型)的值。

这是字典:

{
'1000268201_693b08cb0e'  :  ['A child in a pink dress is climbing up a set  of stairs in an entry way .', 'A girl going into a wooden building .', 'A little girl climbing into a wooden playhouse .', 'A little girl climbing the stairs to her playhouse .', 'A little girl in a pink dress going into a wooden cabin .'],
'101654506_8eb26cfb60'   :  ['A brown and white dog is running through the snow .', 'A dog is running in the snow', 'A dog running through snow .', 'a white and brown dog is running through a snow covered field .', 'The white and brown dog is running over the surface of the snow .']
}

我为上面的字典编写的代码,键为1000268201_693b08cb0e101654506_8eb26cfb60,其值作为句子列表为:

for i in mapping:
    for j in range(0,len(mapping[i])):
        mapping[i][j]=mapping[i][j].lower();

什么是完成上述工作的有效而简洁的方法?

1 个答案:

答案 0 :(得分:1)

您可能还可以简化一点:

my_dict = {'some_key': ['A', 'B', 'C'], 'another_key': ['D', 'E', 'F']}

for key in my_dict:
    my_dict[key] = [x.lower() for x in my_dict[key]]

这省去了致电len()的麻烦。

相关问题