简体   繁体   English

我无法将此 Python 函数转换为 Swift 函数

[英]I'm having trouble converting this Python function to a Swift function

I'm working on this blackjack app for iOS and I'm having trouble getting the function to work properly.我正在开发适用于 iOS 的此二十一点应用程序,但无法使该功能正常工作。 I built it in Python where it works like a charm, but something is wonky when I adapt it.我用 Python 构建了它,它的工作原理很吸引人,但是当我调整它时,有些东西是不稳定的。 The idea is to get every possible value of the cards.这个想法是获得卡片的所有可能的价值。 Once I get that working, I'll sum up the numbers.一旦我开始工作,我会总结这些数字。 So, a [[1,11],[4],[5]] will output [10,20] .因此, [[1,11],[4],[5]]将输出[10,20]

Python code Python代码

init_array = [[1,11],[3],[4],[1,11],[5]]
r = [[]]
for x in init_array:
    t = []
    for y in x:
        for i in r:
            t.append(i + [y])
    r = t
print(r)

Output:输出:

[[1, 3, 4, 1, 5], [11, 3, 4, 1, 5], [1, 3, 4, 11, 5], [11, 3, 4, 11, 5]]

Swift code SWIFT代码

var init_array = [[Int]]()
init_array.append([1,11])
init_array.append([3])
init_array.append([4])
init_array.append([1,11])
init_array.append([5])

var r = [[Int]]()
for x in init_array {
    var t = [Int]()
    for y in x{
        for i in r {
            t.append(contentsOf: (i + [y]))
        }
    }
    r = [t]
}
print(r)

Output:输出:

[[3, 4, 1, 3, 4, 11, 5]]

It's part of a larger function, so creating the init_array variables and printing the r variables for both examples is for testing purposes.它是更大函数的一部分,因此为两个示例创建init_array变量并打印 r 变量是为了测试目的。 The init_array has already been generated by the time it gets to my trouble code. init_array 在它到达我的故障代码时已经生成。

It's probably one simple line of code, but I've been trying to figure this out for days.这可能是一行简单的代码,但我几天来一直试图弄清楚这一点。 I'm relatively new to Swift, so this might take someone more advanced two seconds to figure out.我对 Swift 比较陌生,所以这可能需要更高级的人两秒钟才能弄清楚。

When you initialised r : In python , length of r is 1 where as in swift the length is 0 .当您初始化r :在 python 中, r长度为1 ,而在 swift 中,长度为0 This is because, in python r is initialised with an empty list in inside it [[]] .这是因为,在 python 中, r被初始化为一个空列表,其中[[]] But in swift, it didn't happen [] .但很快,它并没有发生[] This resulted in getting unwanted output.这导致了不需要的输出。

Finally I found a way to fix it by appending an empty list into r in the beginning.最后我找到了一种方法来修复它,方法是在开始时将一个空列表附加到r中。

var r = [[Int]]()
r.append([])

Also t.append(ContentsOf ...) wasn't creating arrays as you wanted.此外t.append(ContentsOf ...)没有按照您的t.append(ContentsOf ...)创建数组。

Here is the final solution这是最终的解决方案

var init_array = [[Int]]()
init_array.append([1,11])
init_array.append([3])
init_array.append([4])
init_array.append([1,11])
init_array.append([5])

var r = [[Int]]()
r.append([])
print(r.count,r) // debuggging part
for x in init_array {
    var t = [[Int]]()
    for y in x{
        for i in r {

            //method 1 
            var dm=[Int]() //for making the kind of arrays u wanted
            dm.append(contentsOf: (i + [y]))  // appending it to array
            t.append(dm)
            // Or
            //method 2
            t+=[(contentsOf: (i + [y]))] as! [[Int]] //HereWeTypeCastToInt
        }
        print(t,y) // debuggging part
    }
    r = t
}
print(r)

Output : [[1, 3, 4, 1, 5], [11, 3, 4, 1, 5], [1, 3, 4, 11, 5], [11, 3, 4, 11, 5]]输出:[[1, 3, 4, 1, 5], [11, 3, 4, 1, 5], [1, 3, 4, 11, 5], [11, 3, 4, 11, 5] ]

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

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