在 python3 的字典内编辑列表中的字符串
Editing string in a list inside a dictionary in python3
如果我有这样的字典:
{'alfa': ['Computer Science'], 'beta': ['book', 'CompUter']}
我想把它变成这样的字典:
{'alfa': ['computer science'], 'beta': ['book', 'computer']}
所以基本上把单词变成小写字母。
为此我知道我需要函数 lower().
但是,我不知道如何访问字典中的单词,所以我可以使用这个功能。
在将列表放入字典之前,我试过这个:
for z in wordlist:
z.lower()
但它对这些词没有任何作用。
my_dict = {'alfa': ['Computer Science'], 'beta': ['book', 'CompUter']}
for key in my_dict:
my_dict[key] = [my_str.lower() for my_str in my_dict[key]]
print(my_dict)
输出:
{'alfa': ['computer science'], 'beta': ['book', 'computer']}
如果我有这样的字典:
{'alfa': ['Computer Science'], 'beta': ['book', 'CompUter']}
我想把它变成这样的字典:
{'alfa': ['computer science'], 'beta': ['book', 'computer']}
所以基本上把单词变成小写字母。 为此我知道我需要函数 lower().
但是,我不知道如何访问字典中的单词,所以我可以使用这个功能。
在将列表放入字典之前,我试过这个:
for z in wordlist:
z.lower()
但它对这些词没有任何作用。
my_dict = {'alfa': ['Computer Science'], 'beta': ['book', 'CompUter']}
for key in my_dict:
my_dict[key] = [my_str.lower() for my_str in my_dict[key]]
print(my_dict)
输出:
{'alfa': ['computer science'], 'beta': ['book', 'computer']}