【发布时间】:2021-09-18 06:39:32
【问题描述】:
我有一个这样的引号列表:
[
{"March 28, 2021", "It only took one call for him to help me into a brand new 2021 it was so AMAZINGLY GOOD IM STILL HAPPY love it"},
{"March 28, 2021", "We had been looking for a 2021 Suburban for about 6 months and no one could find exactly what we wanted! We contacted Adrian at McKaig and he told us he could order one for us! Our Suburban was delivered in 4 weeks and had everything on it that we wanted! Adrian, Brandon, Dennis and Freddie all worked with us to get exactly what we wanted! They made phone calls and deals for us right on the spot and we drove out with a beautiful black Suburban! We will definitely use Adrian and McKaig Chevrolet again! Thank you for a fun car buying experience!"},
...
]
我一直在尝试让这个函数遍历列表中的每个引号,以计算每个引号中的单词数,然后返回原始列表以及映射到每个引号的数字。我被卡住了,现在不知道要寻找什么。如果有人能引导我朝着正确的方向前进,我将不胜感激。
代码如下:
def count(q) do
q
|> String.downcase()
|> String.replace(~r/@|#|\$|%|&|\^|:|_|!|,/u, " ")
|> String.split()
|> Enum.count()
end
def count_added() do
items = get_body() #the list of quotes
Enum.each(items, &count/1)
for x <- items do
Map.put_new_lazy(items, "count", &count/1)
end
end
【问题讨论】:
-
简单地调用
Enum.each(items, &count/1)不会更新items,因为它是不变的。如果您希望items成为上述调用的结果,您需要重新分配它