gpt4 book ai didi

elixir - 从元组列表中选择值 Elixir

转载 作者:行者123 更新时间:2023-12-02 06:30:08 24 4
gpt4 key购买 nike

我正在尝试从元组列表中提取值:

s3_headers = %{headers: [{"x-amz-id-2","yQKurzVIApkxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFBINsPxe+7Vc="},
{"x-amz-request-id", "82xxxxxxxxx23"},
{"Date", "Thu, 25 May 2017 22:03:09 GMT"},
{"Last-Modified", "Thu, 25 May 2017 21:42:28 GMT"},
{"ETag", "\"6f04733333333333333368997\""},
{"x-amz-meta-original_name", "Screenshot from 2016-11-27 17-32-03.png"},
{"Accept-Ranges", "bytes"}, {"Content-Type", ""},
{"Content-Length", "612391"}, {"Server", "AmazonS3"}], status_code: 200}

到目前为止我的做法是这样的:

{"x-amz-meta-original_name", original_name } = s3_headers |> List.keyfind("x-amz-meta-original_name", 0)
{"Content-Length", content_length } = s3_headers |> List.keyfind("Content-Length", 0)
{"Content-Type", content_length } = s3_headers |> List.keyfind("Content-Type", 0)

感觉太复杂了,你能推荐更好的方法吗?

最佳答案

我通常将带有字符串键的元组列表转换为映射。然后您可以使用字符串键进行访问。这将需要更多的前期时间,但每次访问的时间比 Enum.find 少得多

iex(19)> headers = Enum.into s3_headers[:headers], %{}
%{"Accept-Ranges" => "bytes", "Content-Length" => "612391",
"Content-Type" => "", "Date" => "Thu, 25 May 2017 22:03:09 GMT",
"ETag" => "\"6f04733333333333333368997\"",
"Last-Modified" => "Thu, 25 May 2017 21:42:28 GMT", "Server" => "AmazonS3",
"x-amz-id-2" => "yQKurzVIApkxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFBINsPxe+7Vc=",
"x-amz-meta-original_name" => "Screenshot from 2016-11-27 17-32-03.png",
"x-amz-request-id" => "82xxxxxxxxx23"}
iex(20)> original_name = headers["x-amz-meta-original_name"]
"Screenshot from 2016-11-27 17-32-03.png"
iex(21)> content_length = headers["Content-Length"]
"612391"

关于elixir - 从元组列表中选择值 Elixir,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44220937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com