Erlang:对现有列表的列表理解

发布于 2024-10-03 18:48:35 字数 504 浏览 4 评论 0原文

我正在尝试通过列表理解创建一个新列表,但希望将这些新值包含在现有列表中。

更具体地说,我尝试创建一个超出日期的字符串,并且在值之间有一些字符串格式(破折号 - )。如果您愿意使用破折号,现有列表将是一个模板。

这是我到目前为止所得到的:

{Date, Time} = erlang:universaltime().
DateList = tuple_to_list(Date).
DateListString = [ integer_to_list(X) || X < DateList ].
DateListStringConcatenate = lists:flatten(DateListString).

结果应该是这样的 “20101121”

但是,我想要的是 “2010-11-21”

所以我正在考虑 DateListString 理解“理解”到第一个和第二个元素后面带有“-”的现有列表。

任何带有具体代码示例的建议都非常感谢。

I am trying to create a new list via a list comprehension but want those new values to be included in an existing list.

More specifically, I am try to create a string out of the date and will have some string formatting between the values ( a dash - ). The existing list will be a template if you will with the dash.

Here is what I have so far:

{Date, Time} = erlang:universaltime().
DateList = tuple_to_list(Date).
DateListString = [ integer_to_list(X) || X < DateList ].
DateListStringConcatenate = lists:flatten(DateListString).

The result should be something along
"20101121"

But, what I want is
"2010-11-21"

So I am thinking about the DateListString comprehension "comprehending" to an existing list with "-" after the first and second element.

Any suggestions accompanied with concrete code samples much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哎呦我呸! 2024-10-10 18:48:35
1> {{Y,M,D},_} = erlang:universaltime().
{{2010,11,21},{16,42,56}}
2> lists:flatten(io_lib:format("~p-~p-~p", [Y,M,D])).
"2010-11-21"
1> {{Y,M,D},_} = erlang:universaltime().
{{2010,11,21},{16,42,56}}
2> lists:flatten(io_lib:format("~p-~p-~p", [Y,M,D])).
"2010-11-21"
空袭的梦i 2024-10-10 18:48:35

如果您确实希望在列表理解中使用它,那么您可以执行以下操作:

{Date, Time} = erlang:universaltime().
DateList = tuple_to_list(Date).
DateListString = [ [$- | integer_to_list(X)] || X <- DateList ].
[_ | DateListStringConcatenate] = lists:flatten(DateListString).

罗伯托的解决方案是一个更好/更有效的解决方案,但如果您想知道如何使用列表理解来做到这一点,这将是一种方法。

If you really want it in a list comprehension then you could do the following:

{Date, Time} = erlang:universaltime().
DateList = tuple_to_list(Date).
DateListString = [ [$- | integer_to_list(X)] || X <- DateList ].
[_ | DateListStringConcatenate] = lists:flatten(DateListString).

Roberto's is a better/more efficient solution to this but in case you wondered how you might do it with a list comprehension this would be one way.

寒江雪… 2024-10-10 18:48:35

这是一种可能的解决方案,但我觉得这不是一个优雅的解决方案。此外,它不使用列表理解。

1> {Date, Time} = erlang:universaltime().
{{2010,11,21},{14,51,23}}
2> DateList = tuple_to_list(Date).
[2010,11,21]
3> DateListString = lists:zipwith(fun(X,Y) -> integer_to_list(X) ++ Y end, DateList, ["-","-",""]).           
["2010-","11-","21"]
4> DateListStringConcatenate = lists:flatten(DateListString).
"2010-11-21"

This is a possible solution, but I feel that it is not an elegant one. Also, it does not use list comprehension.

1> {Date, Time} = erlang:universaltime().
{{2010,11,21},{14,51,23}}
2> DateList = tuple_to_list(Date).
[2010,11,21]
3> DateListString = lists:zipwith(fun(X,Y) -> integer_to_list(X) ++ Y end, DateList, ["-","-",""]).           
["2010-","11-","21"]
4> DateListStringConcatenate = lists:flatten(DateListString).
"2010-11-21"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文