本文介绍了为什么Elixir的Enum.count不返回数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下输入内容:

[
  [
    {"title", "Aaaaaaaaa aaaaaaaaaaa aa aaa oaaaaaaaa"},
    {"title", "Aaaaaaa aa aaaa aaa.aa"},
    {"title", "Aaaaaaaaaaaa Aaaaaaa, aaa Aaaaaaa"},
    {"title", "Aaaa Aaaaaaaa aaaa Aaaaa"},
    {"title", "Aaaaaaa Aaaaaaa aaaa Aaaaa"},
    {"title", "Aaaaa Aaaaaaa aa Aaaaaa"},
    {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaaaaa, Aaa Aaaaaaa"},
    {"title",
     "Aaaaaaaaa Aaaaaaa Aaaaaa aaaaaaaa Aaaaaaaaaaaaa aaa Aaaaaaaaaa Aaaaaa Aaaaaa Aaaaaaa"},
    {"title", "Aaaaaaaaaaaa Aaaaaaa"},
    {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaa"}
  ],
  [
    {"url",
     "http://localhost:3000/aaaaaaaaa-aaaaaaaaaaa-aa-aaa-aaaaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaa-aa-aaaa-aaa-aa/"},
    {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa-aaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaa-aaaaaaaa-aaaa-aaaaa/"},
    {"url", "http://localhost:3000/aaaaaaa-aaaaaaa-aaaa-aaaaa/"},
    {"url", "http://localhost:3000/aaaaa-aaaaaaa-aa-aaaaaa/"},
    {"url",
     "http://localhost:3000/aaaaaaaaa-aaaa-aaaa-aaaaaaa-aaa-aaaaaaa/"},
    {"url",
     "http://localhost:3000/aaaaaaaaa-aaaaaaa-aaaaaa-aa-aaa-aaaaaaaaaaaaa-aaa-aaaaaaaaaa-aaaaa-aaaaaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaaaa-aaaaa-aaaa-aaaa/"}
  ]
]

然后启动iex,并将其分配给变量,然后执行计数映射,例如在此iex会话中:

and I start iex, and assign it to a variable, then perform a map of a count, like in this iex session:

Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> input = [
...(1)>   [
...(1)>     {"title", "Aaaaaaaaa aaaaaaaaaaa aa aaa oaaaaaaaa"},
...(1)>     {"title", "Aaaaaaa aa aaaa aaa.aa"},
...(1)>     {"title", "Aaaaaaaaaaaa Aaaaaaa, aaa Aaaaaaa"},
...(1)>     {"title", "Aaaa Aaaaaaaa aaaa Aaaaa"},
...(1)>     {"title", "Aaaaaaa Aaaaaaa aaaa Aaaaa"},
...(1)>     {"title", "Aaaaa Aaaaaaa aa Aaaaaa"},
...(1)>     {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaaaaa, Aaa Aaaaaaa"},
...(1)>     {"title",
...(1)>      "Aaaaaaaaa Aaaaaaa Aaaaaa aaaaaaaa Aaaaaaaaaaaaa aaa Aaaaaaaaaa Aaaaaa Aaaaaa Aaaaaaa"},
...(1)>     {"title", "Aaaaaaaaaaaa Aaaaaaa"},
...(1)>     {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaa"}
...(1)>   ],
...(1)>   [
...(1)>     {"url",
...(1)>      "http://localhost:3000/aaaaaaaaa-aaaaaaaaaaa-aa-aaa-aaaaaaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaaaaa-aa-aaaa-aaa-aa/"},
...(1)>     {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa-aaa-aaaaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaa-aaaaaaaa-aaaa-aaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaaaaa-aaaaaaa-aaaa-aaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaaa-aaaaaaa-aa-aaaaaa/"},
...(1)>     {"url",
...(1)>      "http://localhost:3000/aaaaaaaaa-aaaa-aaaa-aaaaaaa-aaa-aaaaaaa/"},
...(1)>     {"url",
...(1)>      "http://localhost:3000/aaaaaaaaa-aaaaaaa-aaaaaa-aa-aaa-aaaaaaaaaaaaa-aaa-aaaaaaaaaa-aaaaa-aaaaaa-aaaaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaaaaaaa-aaaaa-aaaa-aaaa/"}
...(1)>   ]
...(1)> ]
[
  [
    {"title", "Aaaaaaaaa aaaaaaaaaaa aa aaa oaaaaaaaa"},
    {"title", "Aaaaaaa aa aaaa aaa.aa"},
    {"title", "Aaaaaaaaaaaa Aaaaaaa, aaa Aaaaaaa"},
    {"title", "Aaaa Aaaaaaaa aaaa Aaaaa"},
    {"title", "Aaaaaaa Aaaaaaa aaaa Aaaaa"},
    {"title", "Aaaaa Aaaaaaa aa Aaaaaa"},
    {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaaaaa, Aaa Aaaaaaa"},
    {"title",
     "Aaaaaaaaa Aaaaaaa Aaaaaa aaaaaaaa Aaaaaaaaaaaaa aaa Aaaaaaaaaa Aaaaaa Aaaaaa Aaaaaaa"},
    {"title", "Aaaaaaaaaaaa Aaaaaaa"},
    {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaa"}
  ],
  [
    {"url", "http://localhost:3000/aaaaaaaaa-aaaaaaaaaaa-aa-aaa-aaaaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaa-aa-aaaa-aaa-aa/"},
    {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa-aaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaa-aaaaaaaa-aaaa-aaaaa/"},
    {"url", "http://localhost:3000/aaaaaaa-aaaaaaa-aaaa-aaaaa/"},
    {"url", "http://localhost:3000/aaaaa-aaaaaaa-aa-aaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaaaa-aaaa-aaaa-aaaaaaa-aaa-aaaaaaa/"},
    {"url",
     "http://localhost:3000/aaaaaaaaa-aaaaaaa-aaaaaa-aa-aaa-aaaaaaaaaaaaa-aaa-aaaaaaaaaa-aaaaa-aaaaaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaaaa-aaaaa-aaaa-aaaa/"}
  ]
]
iex(2)> input |> Enum.map(fn item -> Enum.count(item) end)
'\n\n'

不清楚上面的iex会话为什么不返回包含[10,10]的列表。我究竟做错了什么?

It's not obvious why the above iex session does not return a list containing [10,10]. What am I doing wrong?

谢谢!

推荐答案

iex使用协议 IO.Inspect ,它将结果视为 charlist ,因此将 \n\n 视为结果。如果使用此行:

iex uses the protocol IO.Inspect, which sees the result as a charlist, hence the \n\n. If you use this line:

iex> input |> Enum.map(fn item -> Enum.count(item) end) |> hd |> to_string |> String.to_integer

您将获得:

iex> 10

显示此列表时遇到的问题与之相同:

it is the same issue that you have when you display this list:

iex> list = [7,8,9]
'\a\b\t'

解决方案:您可以在地图中添加数字(例如0),以强制结果显示为数字列表:

Solution: you can add a number such as 0 into your map, to force the result to appear as list of number:

iex> [0] ++ (input |> Enum.map(fn item -> Enum.count(item) end))
[0,10,10]

该主题已经讨论过

这篇关于为什么Elixir的Enum.count不返回数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 13:28