[英]How to use map to lowercase strings in a dictionary?
我正在尝试使用Python 3.6中的map
, filter
和reduce
。 我想要做的是,给定一个字典列表 ,将与某个键相关联的所有值更改为小写值。 例如:
message_one = {"content": "I'm glad I know sign language, it's pretty handy."}
message_two = {"content": "I am on a seafood diet. Every time I see food, I eat it."}
message_three = {"content": "Labyrinths are amazing."}
messages = [message_one , message_two , message_three]
print(to_lowercase(tweets))
#to_lowercase should just return the a list of dictionaries, but content lower-cased.
我首先尝试使用地图 。
def to_lowercase(messages):
lower_case = map(lambda x: x["content"].lower(), messages)
return lower_case
但是,这似乎只返回列表中所有内容消息的列表,并且不会保留字典格式。 我不认为在这种情况下reduce
是正确的,因为我不打算在最后返回单个值,并且filter
在这里似乎没有意义。
我如何使用map
, reduce
或filter
来完成这项工作?
简单的解决方案,使与小写值新类型的字典列表:
dicts = [{k:v.lower() for k,v in d.items()} for d in messages]
print(dicts)
输出:
[{'content': "i'm glad i know sign language, it's pretty handy."}, {'content': 'i am on a seafood diet. every time i see food, i eat it.'}, {'content': 'labyrinths are amazing.'}]
(注意,这个答案假设您正在使用Python 2;如果您使用的是Python 3,请考虑map()
返回一个迭代器 ,您需要添加某种循环来查看结果)。
如果你坚持使用map()
,那么你想要创建一个新函数来应用于每个现有字典:
def dict_lowercase_content(d):
"""Produces a copy of `d` with the `content` key lowercased"""
copy = dict(d)
if 'content' in copy:
copy['content'] = copy['content'].lower()
return copy
def to_lowercase(tweets):
return map(dict_lowercase_content, tweets)
dict_lowercase_content()
不假设字典中存在哪些键; 它将创建所有键的浅表副本, 如果存在content
键,则它是小写的。
当然,如果你可以确定只有content
键是重要的并且始终存在,你可以用这个键创建全新的词典;
def to_lowercase(tweets):
return map(lambda d: {'content': d['content'].lower()}, tweets)
如果就地更新字典很好(这会更有效),只需使用循环:
def to_lowercase(tweets):
for tweet in tweets:
if 'content' in tweet:
tweet['content'] = tweet['content'].lower()
请注意,此函数返回None
! 这是Python惯例; 当就地修改可变对象时,不要再次返回这些对象,因为调用者已经有一个引用。
您不能对此作业使用reduce()
或filter()
。
filter()
从iterable中选择元素 。 你没有选择,你正在改变。
reduce()
聚合元素 ; 输入中的每个元素与运行结果一起传递给函数; 函数返回的任何内容都被视为更新结果。 想想总结,连接或遍历树。 同样,你没有聚合,你正在改变。
带map
:
map(lambda x: {'content': x['content'].lower()}, messages)
没有map
:
[{'content': x['content'].lower()} for x in messages]
没有map
,但更健壮:
[{y: x[y].lower()} for x in messages for y in x]
map()是一个内置的高阶函数,用于转换集合。 我们提供一个匿名函数(lambda)来执行map,而不是将集合作为参数本身提供。 实现目标的一种方法是:
transformed_messages = list(map(lambda x: {'content':x['content'].lower()}, messages))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.