如何将双精度浮点数列表减少为字符串?

huangapple go评论73阅读模式
英文:

How to reduce the list of doubles to string?

问题

我对Java 8和Kotlin中的reduce和fold等函数式方法还不熟悉。我想要将以下内容进行reduce操作 -

    List<List<List<Double>>> boundingPolygon = [[[-125.48845080566404,47.94508483691371],[-124.96110705566404,42.309040799653665],[-117.13884143066404,45.04173793121063],[-118.36931018066404,48.93624688577435],[-125.48845080566404,47.94508483691371]]];

将其简化为一个表示多个坐标串联的字符串 -

    "-118.359053 33.931562,-118.372443 33.946939,-118.369053 33.951562,-118.337612 33.944342,-118.342012 33.944042,-118.359053 33.931562"

尝试了以下方法 -

    val polygonCoordinates = boundingPolygon.first().reduce { acc, element ->
                        acc + "${element[0]} ${element[1]}"
                    }

但是这不起作用。
英文:

I'm new to functional methods like reduce and fold in java 8 and Kotlin. I want to reduce -

List&lt;List&lt;List&lt;Double&gt;&gt;&gt; boundingPolygon = [[[-125.48845080566404,47.94508483691371],[-124.96110705566404,42.309040799653665],[-117.13884143066404,45.04173793121063],[-118.36931018066404,48.93624688577435],[-125.48845080566404,47.94508483691371]]]; 

to single string that represents one String concatenated by coordinates -

&quot;-118.359053 33.931562,-118.372443 33.946939,-118.369053 33.951562,-118.337612 33.944342,-118.342012 33.944042,-118.359053 33.931562&quot;

Trying to do -

val polygonCoordinates = boundingPolygon.first().reduce { acc, element -&gt;
                    acc + &quot;${element[0]} ${element[1]}&quot;
                }

This is not working.

答案1

得分: 3

在您的reduce操作中,acc的类型为List&lt;Double&gt;,而不是String。查看reduce函数的签名,您就应该明白原因。这是我提出的实现您想要的方式:

coords.first().joinToString(", ") { (x, y) -> "$x $y" }

我使用了解构来从坐标列表中提取第一个和第二个值。因此,这仅适用于二维坐标。

英文:

The acc in your reduce operation is of type List&lt;Double&gt;, not String. Look at the reduce function signature and you should understand why. Here's what I propose to do what you want:

coords.first().joinToString(&quot;, &quot;) { (x, y) -&gt; &quot;$x $y&quot; }

I used destructuring to extract the first and second values from the coordinate lists. So this will only work with 2D coordinates.

答案2

得分: 1

代替将其缩小,你应该将它们添加到 StringBuilder 中,当您进行多个操作(例如连接大量字符串)时,它会更有效率:

val boundingPolygon = listOf(
    listOf(
        listOf(-125.48845080566404, 47.94508483691371),
        listOf(-124.96110705566404, 42.309040799653665),
        listOf(-117.13884143066404, 45.04173793121063),
        listOf(-118.36931018066404, 48.93624688577435),
        listOf(-125.48845080566404, 47.94508483691371)
    )
)

val sb = StringBuilder()
for (nestedList in boundingPolygon) {
    for (innerNestedList in nestedList) {
        sb.append(innerNestedList.joinToString(" "))
        sb.append(',')
    }
}
if (sb.isNotEmpty()) sb.deleteCharAt(sb.lastIndex)

println(sb)
// 输出:-125.48845080566404 47.94508483691371,-124.96110705566404 42.309040799653665,-117.13884143066404 45.04173793121063,-118.36931018066404 48.93624688577435,-125.48845080566404 47.94508483691371

// val stringRepresentation = sb.toString()  // 用作字符串数据类型的进一步用途
英文:

Instead of reducing it, you should just add those into a StringBuilder, which is efficient when you are doing multiple operations (like concatenating a ton of Strings):

val boundingPolygon = listOf(
    listOf(
        listOf(-125.48845080566404, 47.94508483691371),
        listOf(-124.96110705566404, 42.309040799653665),
        listOf(-117.13884143066404, 45.04173793121063),
        listOf(-118.36931018066404, 48.93624688577435),
        listOf(-125.48845080566404, 47.94508483691371)
    )
)

val sb = StringBuilder()
for (nestedList in boundingPolygon) {
    for (innerNestedList in nestedList) {
        sb.append(innerNestedList.joinToString(&quot; &quot;))
        sb.append(&#39;,&#39;)
    }
}
if (sb.isNotEmpty()) sb.deleteCharAt(sb.lastIndex)

println(sb)
// Output: -125.48845080566404 47.94508483691371,-124.96110705566404 42.309040799653665,-117.13884143066404 45.04173793121063,-118.36931018066404 48.93624688577435,-125.48845080566404 47.94508483691371

// val stringRepresentation = sb.toString()  // for further use as String data-type

答案3

得分: 1

不要使用Reduce,可以使用flatMap。它会对你有帮助。

List<List<List>> boundingPolygon = List.of(List.of(List.of(-124.96110705566404, 42.309040799653665)
, List.of(-117.13884143066404, 45.04173793121063)
, List.of(118.36931018066404, 48.93624688577435)
));
var l = boundingPolygon.stream().flatMap(lists -> lists.stream().flatMap(doubles -> doubles.stream())).collect(Collectors.toList());
System.out.println(l);

它会输出如下结果。


[-124.96110705566404, 42.309040799653665, -117.13884143066404, 45.04173793121063, 118.36931018066404, 48.93624688577435]

尝试使用上述代码,这会对你有帮助。

英文:

Instead of Reduce, you can use flatMap. It will help you.

        List&lt;List&lt;List&lt;Double&gt;&gt;&gt; boundingPolygon = List.of(List.of(List.of(-124.96110705566404, 42.309040799653665)
            , List.of(-117.13884143066404, 45.04173793121063)
            , List.of(118.36931018066404, 48.93624688577435)
    ));
        var l = boundingPolygon.stream().flatMap(lists -&gt; lists.stream().flatMap(doubles -&gt; doubles.stream())).collect(Collectors.toList());
        System.out.println(l);

It will print an output as below.


[-124.96110705566404, 42.309040799653665, -117.13884143066404, 45.04173793121063, 118.36931018066404, 48.93624688577435]

Try above code, this will help you.

huangapple
  • 本文由 发表于 2020年8月21日 15:34:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63518483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定