简体   繁体   English

如何将一个双精度数组的数组转换为字符串,Swift

[英]How to turn an array of an array of doubles into string, Swift

I have an array of array of doubles. 我有一系列双打数组。 How do you convert that to a single string printed on the console? 如何将其转换为打印在控制台上的单个字符串?

I need to create a function that takes in an array of arrays of doubles as a parameter and returns a string object. 我需要创建一个函数,它接受一个双精度数组作为参数并返回一个字符串对象。 Then loop through each of the inner arrays and concatenate each element of the inner arrays into a String object. 然后循环遍历每个内部数组,并将内部数组的每个元素连接成一个String对象。 But I'm not sure how to go about that. 但我不知道该怎么做。

var array1 = [2.6, 6.7, 7.2, 4.1, 3.1]
var array2 = [1.2, 3.5, 2.8, 4.5, 6.4]
var array3 = [1.2, 1.3, 1.4, 1.5, 1.6]

var nestedArray = [array1, array2, array3]

This is the code I have... but i don't know how to do a for loop that would give me my answer... 这是我的代码...但我不知道如何做一个for循环,它会给我我的答案......

func nestedFunction(nestedArray: [[Double]]) -> String {
    var stringVar: String = ""

    ( for loop here... )

    return stringVar
}

print(nestedArrayFunction(nestedArray))

The expected output should be a string object, with no brackets 预期的输出应该是一个字符串对象,没有括号

If you want it without the brackets: 如果你想要它没有括号:

let string = nestedArray.flatMap { (array) in
    array.flatMap { String($0) }.joinWithSeparator(",")
}.joinWithSeparator(",")

Output: 输出:

"2.6,6.7,7.2,4.1,3.1,1.2,3.5,2.8,4.5,6.4,1.2,1.3,1.4,1.5,1.6"

Mind that using , as separator isn't localisation proof, and in French locale would result in a string like "2,6,6,7,7,2" and so on. 请注意,使用,作为分隔符不是本地化证明,并且在法语区域设置中将产生类似“2,6,6,7,7,2”的字符串,依此类推。

Array conforms to CustomStringConvertible , so you can just use its description property: Array符合CustomStringConvertible ,因此您只需使用其description属性:

nestedArray.description

output: 输出:

[[2.6000000000000001, 6.7000000000000002, 7.2000000000000002, 4.0999999999999996, 3.1000000000000001], [1.2, 3.5, 2.7999999999999998, 4.5, 6.4000000000000004], [1.2, 1.3, 1.3999999999999999, 1.5, 1.6000000000000001]] [[2.6000000000000001,6.7000000000000002,7.2000000000000002,4.0999999999999996,3.1000000000000001],[1.2,3.5,2.7999999999999998,4.5,6.4000000000000004],[1.2,1.3,1.3999999999999999,1.5,1.6000000000000001]]

This is what the regular print function uses to print such arrays. 这是常规print功能用于打印此类数组的内容。

First, you will need to concatenate, or combine, the arrays you have. 首先,您需要连接或组合您拥有的阵列。 You can do that simply by creating another array, like this; 你可以通过创建另一个数组来做到这一点,就像这样;

let fullArray = array1 + array2 + array3

Next, you can print the full array, 接下来,您可以打印完整的阵列,

print(fullArray)

Which would give you something like 哪个会给你类似的东西

[2.6000000000000001, 6.7000000000000002, 7.2000000000000002, 4.0999999999999996, 3.1000000000000001, 1.2, 3.5, 2.7999999999999998, 4.5, 6.4000000000000004]

or you can create a string representation of this array using the .joinWithSeparator(String: String) method, like so 或者您可以使用.joinWithSeparator(String:String)方法创建此数组的字符串表示形式,如下所示

let stringRepresentation = fullArray.joinWithSeparator(",")
print(stringRepresentation)

Hope this helps! 希望这可以帮助!


From the description in your question it seems as if the argument of nestedFunction(...) should be an array of arrays with double valued elements ( [[Double]] ) rather than an array with double valued elements ( [Double] ). 从您的问题中的描述看来,似乎nestedFunction(...)的参数应该是具有双值元素[[Double]] )的数组数组,而不是具有双值元素数组[Double] )。

You can make use of .flatten() to your simply nested array nestedArray in nestedFunction(...) , and thereafter eg reduce to transform the Double valued elements of the flattened array to one concenated String . 你可以将.flatten()用于nestedFunction(...)简单嵌套的数组nestedArray ,然后例如reduce将flattened数组的Double值元素转换为一个同心String

var array1 = [2.6, 6.7, 7.2, 4.1, 3.1]
var array2 = [1.2, 3.5, 2.8, 4.5, 6.4]
var array3 = [1.2, 1.3, 1.4, 1.5, 1.6]

var nestedArray = [array1, array2, array3]

func nestedFunction (nestedArray: [[Double]])-> String {
    return String(nestedArray
        .flatten()
        .reduce("") { $0 + ", " + String($1) }
        .characters.dropFirst(2))
}

let asString = nestedFunction(nestedArray)
    // asString = "2.6, 6.7, 7.2, 4.1, 3.1, 1.2, 3.5, 2.8, 4.5, 6.4, 1.2, 1.3, 1.4, 1.5, 1.6"

As an alternative, if you're set on using a for loop, you can use for ... in on the flattened array, eg: 作为替代方案,如果你设置使用for循环,你可以在flattened数组上使用for ... in ,例如:

var array1 = [2.6, 6.7, 7.2, 4.1, 3.1]
var array2 = [1.2, 3.5, 2.8, 4.5, 6.4]
var array3 = [1.2, 1.3, 1.4, 1.5, 1.6]

var nestedArray = [array1, array2, array3]

func nestedFunction (nestedArray: [[Double]])-> String {
    var stringVar: String = ""
    var isFirstElement = true
    for elem in nestedArray.flatten() {
        stringVar += (isFirstElement ? "" : ", ") + String(elem)
        isFirstElement = false
    }
    return stringVar
}

let asString = nestedFunction(nestedArray)
    // asString = "2.6, 6.7, 7.2, 4.1, 3.1, 1.2, 3.5, 2.8, 4.5, 6.4, 1.2, 1.3, 1.4, 1.5, 1.6"

Note that due to limited floating point precision, some double values might end up with a "messy" String representation (eg 2.6 might end up as 2.6000000000000001 ) when using the direct String initializer ( String($1) and String(elem) above, in the first and second method, respectively). 请注意,由于有限的浮点精度,当使用直接String初始值设定项( String($1)String(elem) )时,某些双值可能会以“杂乱的” String表示形式结束(例如2.6可能最终为2.6000000000000001 ),第一种和第二种方法,分别)。 To redeem this you could set a fixed number of fraction digits for the String representation of your Double values, using the following String initializer: 要兑换它,您可以使用以下String初始值设定项为Double值的String表示设置固定数量的小数位数:

String(format: "%.3f", myDoubleValue)
    /*             \
           number of fraction digits (3 here) */

Eg, replace String($1) and String(elem) in the methods above by String(format: "%.3f", $1) and String(format: "%.3f", elem) , respectively, for some number of fraction digits of your choice. 例如,替换String($1)String(elem)在上述方法由String(format: "%.3f", $1)String(format: "%.3f", elem)分别,对于一些数量分数的您选择的数字。 The Double values will be rounded to the number of supplied fraction digits. Double值将四舍五入为提供的小数位数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM