category_urls = [
"https://thepiratebay.org/browse/100/0/3",
"https://thepiratebay.org/browse/200/0/3",
"https://thepiratebay.org/browse/300/0/3",
"https://thepiratebay.org/browse/400/0/3",
"https://thepiratebay.org/browse/500/0/3",
"https://thepiratebay.org/browse/600/0/3"
]
result =
category_urls
|> Enum.map(fn category_url ->
1..50
|> Enum.map(fn i -> String.replace(category_url, "/0/", "/#{i}/") end)
end)
我正在尝试生成我需要抓取的网址映射。
上面的代码为我生成了字符串映射图。我只想将其展平为一个简单的字符串映射。
我如何在Elixir中做到这一点?
使用Enum.flat_map/2
。
category_urls = [
"https://thepiratebay.org/browse/100/0/3",
"https://thepiratebay.org/browse/200/0/3",
"https://thepiratebay.org/browse/300/0/3",
"https://thepiratebay.org/browse/400/0/3",
"https://thepiratebay.org/browse/500/0/3",
"https://thepiratebay.org/browse/600/0/3"
]
result =
category_urls
|> Enum.flat_map(fn category_url ->
1..50
|> Enum.map(fn i -> String.replace(category_url, "/0/", "/#{i}/") end)
end)
end
但是,使用comprehension
会使代码更简单。
for i <- 1..6, j <- 1..50, do: "https://thepiratebay.org/browse/#{i}00/#{j}/3"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句