将K长度的列表分成L个子列表,这些子列表是“偶数”的。尽可能,即使 K/L 留下余数
我不知道有更好的方式来表达我正在寻找的内容,所以请耐心等待。
假设我有一个包含 17 个元素的列表。为了简洁起见,我们将此列表表示为 ABCDEFGHIJKLMNOPQ
。如果我想将其分为 7 个足够“均匀”的子列表,它可能如下所示:
ABC
DE
FGH
< code>IJ KL
MNO
PQ
这里,每个子列表的长度为3, 2, 3, 2, 2, 3, 2.最大长度仅比最小长度多一位: ABC
DE
FGH
I
JKL
MN
OPQ
也有七个子列表,但这里的长度范围是两个。
此外,检查每对 3 之间有多少个 2:这遵循 RANGE ≤ 1 的相同规则。 ABC
DEF
GH
IJ
KLM
NO
PQ
也是 1,但它们不平衡:3, 3, 2, 2 , 3、2、2。理想情况下,如果以这种方式不断减少子列表,数字之间的偏差永远不会超过 1。
当然,以这种方式将列表“均匀”划分为子列表的方法不止一种。我并不是在寻找一套详尽的解决方案 - 如果我可以在 Python 中为任意长度的列表和任意数量的子列表找到一个解决方案,这对我来说就足够了。问题是,在解决这样的问题时,我什至不知道从哪里开始。有谁知道我在找什么?
I don't know of a better way to word what I'm looking for, so please bear with me.
Let's say that I have a list of 17 elements. For the sake of brevity we'll represent this list as ABCDEFGHIJKLMNOPQ
. If I wanted to divide this into 7 sufficiently "even" sub-lists, it might look like this:
ABC
DE
FGH
IJ
KL
MNO
PQ
Here, the lengths of each sub-list are 3, 2, 3, 2, 2, 3, 2. The maximum length is only one more than the minimum length: ABC
DE
FGH
I
JKL
MN
OPQ
has seven sub-lists as well, but the range of lengths is two here.
Furthermore, examine how many 2's separate each pair of 3's: this follows the same rule of RANGE ≤ 1. The range of lengths in ABC
DEF
GH
IJ
KLM
NO
PQ
is 1 as well, but they are imbalanced: 3, 3, 2, 2, 3, 2, 2. Ideally, if one were to keep reducing the sub-list in such a fashion, the numbers would never deviate from one another by more than one.
Of course, there is more than one way to "evenly" divide a list into sub-lists in this fashion. I'm not looking for an exhaustive set of solutions - if I can get one solution in Python for a list of any length and any number of sub-lists, that's good enough for me. The problem is that I don't even know where to begin when solving such a problem. Does anyone know what I'm looking for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你有一个长度为 N 的列表,并且你想要一定数量的子列表 S,在我看来,你应该从余数除法开始。对于 N == 17 和 S == 7,您有 17 // 7 == 2 和 17 % 7 == 3。因此您可以从 7 个长度值 2 开始,但要知道您需要将长度增加 3值除以 1 以处理余数。由于长度值列表的长度为 7,并且有 3 个值要递增,因此您可以计算 X = 7 / 3 并将其用作步幅:递增第 0 项,然后递增 int(X) 项、int(2 *X) 项目,等等。
如果这对您不起作用,我建议您阅读 Skiena 的一本名为算法设计手册的书,并仔细研究集合和树算法。
http://www.algorist.com/
If you have a list of length N, and you want some number of sub-lists S, it seems to me that you should start with a division with remainder. For N == 17 and S == 7, you have 17 // 7 == 2 and 17 % 7 == 3. So you can start with 7 length values of 2, but know that you need to increment 3 of the length values by 1 to handle the remainder. Since your list of length values is length 7, and you have 3 values to increment, you could compute X = 7 / 3 and use that as a stride: increment the 0th item, then the int(X) item, the int(2*X) item, and so on.
If that doesn't work for you, I suggest you get a book called The Algorithm Design Manual by Skiena, and look through the set and tree algorithms.
http://www.algorist.com/
请参阅 http://docs.python.org/library/itertools.html< 中的“grouper”示例/a>
See the "grouper" example at http://docs.python.org/library/itertools.html