英文:
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("0" + 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("1" + 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("0" + 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("1" + 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"{bin(n)[2:]:0>{k}}" for n in numbers])
for k,g in groupby(numbers,key=lambda x: x.count("1")):
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: ['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']}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论