Swift中与Python中的2D数组/列表的“for”语法等效。

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

Swift equivalent of Python "for" syntax with a 2D array/list

问题

以下是翻译好的部分:

我有一个在Python中找到的代码片段,它适合我的需求,我正在尝试理解它的含义,以便我可以用Swift编写它。我不明白的部分是两行for str in DP[..][..]

我遇到的问题是真正迭代的是什么。DP[len-1][n]似乎是一个单元格的值,而不是一个可以迭代的数组。

我尝试了许多不同的方法,但不会在这里发布它们,因为我对Python语法不了解,所以在黑暗中进行尝试。我尝试在Python参考资料中查找这种语法,但没有找到有用的信息。我还查看了提供的其他实现,但它们使用了一种不同的(昂贵的)技术,这将是繁琐的。

这来自以下文章...
<https://www.geeksforgeeks.org/find-combinations-k-bit-numbers-n-bits-set-1-n-k-sorted-order/>

有问题的结构是:...

for len in range(1, k+1):
    for n in range(1, len+1):
        # prefix 0 to all combinations of length len-1
        # with n ones
        for str in DP[len-1][n]:
            DP[len][n].append(&quot;0&quot; + str)

        # prefix 1 to all combinations of length len-1
        # with n-1 ones
        for str in DP[len-1][n-1]:
            DP[len][n].append(&quot;1&quot; + str)
英文:

I have a code fragment that I have found that suits my needs in Python, and I am trying to understand its meaning so that I can write it in Swift. The lines I do not get are the two lines for str in DP[..][..].

The trouble I am having is understanding what it is actually iterating over. DP[len-1][n] seems to be the value of a cell, not an array that can be iterated over.

I have tried a number of different approaches but will not post them here as I am hacking in the dark not understanding the Python syntax. I have tried to find this syntax in Python references but have not found anything useful. I have also looked at the other implementations offered but they use a different (costly) technique that will be burdensome.

The article this came from is...
<https://www.geeksforgeeks.org/find-combinations-k-bit-numbers-n-bits-set-1-n-k-sorted-order/>

The construct in question is:...

for len in range(1, k+1):
    for n in range(1, len+1):
        # prefix 0 to all combinations of length len-1
        # with n ones
        for str in DP[len-1][n]:
            DP[len][n].append(&quot;0&quot; + str)

        # prefix 1 to all combinations of length len-1
        # with n-1 ones
        for str in DP[len-1][n-1]:
            DP[len][n].append(&quot;1&quot; + str)

答案1

得分: 0

我重写了链接的代码,使用了itertools中的函数。

这将创建一个字典,其中位数作为键,具有此位数的数字列表作为值,它将打印如下内容:

{0: ['00000'],
 1: ['00001', '00010', '00100', '01000', '10000'],
 2: ['00011', '00101', '00110', '01001', '01010', '01100', '10001', '10010', '10100', '11000'],
 3: ['00111', '01011', '01101', '01110', '10011', '10101', '10110', '11001', '11010', '11100'],
 4: ['01111', '10111', '11011', '11101', '11110'],
 5: ['11111']}
英文:

I rewrote the linked code to use the functions from itertools.
I don't know if this helps you in any way converting it to Swift code but for others it might be useful:

from itertools import *
from pprint import *
from collections import *

k=5
d = defaultdict(list)
numbers = list(range(2**k))
numbers = sorted([f&quot;{bin(n)[2:]:0&gt;{k}}&quot; for n in numbers])
for k,g in groupby(numbers,key=lambda x: x.count(&quot;1&quot;)):
    d[k].extend(list(g))
pprint(dict(d))

A dictionary will be created with the bit count as key and the list of numbers having this bit count as values, it will print this:

{0: [&#39;00000&#39;],
 1: [&#39;00001&#39;, &#39;00010&#39;, &#39;00100&#39;, &#39;01000&#39;, &#39;10000&#39;],
 2: [&#39;00011&#39;,
     &#39;00101&#39;,
     &#39;00110&#39;,
     &#39;01001&#39;,
     &#39;01010&#39;,
     &#39;01100&#39;,
     &#39;10001&#39;,
     &#39;10010&#39;,
     &#39;10100&#39;,
     &#39;11000&#39;],
 3: [&#39;00111&#39;,
     &#39;01011&#39;,
     &#39;01101&#39;,
     &#39;01110&#39;,
     &#39;10011&#39;,
     &#39;10101&#39;,
     &#39;10110&#39;,
     &#39;11001&#39;,
     &#39;11010&#39;,
     &#39;11100&#39;],
 4: [&#39;01111&#39;, &#39;10111&#39;, &#39;11011&#39;, &#39;11101&#39;, &#39;11110&#39;],
 5: [&#39;11111&#39;]}

huangapple
  • 本文由 发表于 2023年7月27日 22:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780628.html
匿名

发表评论

匿名网友

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

确定