如何将列表分成n个相等的部分,python
给定(任何)单词列表lst
,我应该将其分为 10 个相等的部分。
x = len(lst)/10
如何给这些部分变量命名?
在输出中,我需要 10 个变量(part1、part2...part10
),其中包含 x
个单词。
Given (any) list of words lst
I should divide it into 10 equal parts.
x = len(lst)/10
how to give these parts variable names?
In the output I need 10 variables (part1, part2... part10
) with x
number of words in it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
单行返回列表列表,给定列表和块大小:
测试:
更新:
好吧,在这种情况下,你可以尝试:
我确信这可以改进,但我感觉很懒。 :-)
One-liner returning a list of lists, given a list and the chunk size:
Testing:
Update:
Well, in this case, you can try:
I'm sure this can be improved but I'm feeling lazy. :-)
为了达到与 Paulo 的更新相同的结果(将列表分为 n 个大小仅相差 1 的块),以下是使用递归的优雅解决方案。
例子:
To achieve the same result as Paulo's update (divide a list into n chunks with size only differing by 1), the following is an elegant solution using recursion.
Example:
请参阅此问题了解如何生成列表的相等块。然后,如果您确实需要将它们放在单独的变量中,您可以这样做:
但我建议使代码更通用,而不是将其硬编码为 10 个部分。
See this question for how to generate equal chunks of a list. Then, if you really need them in separate variables, you can do:
But I would recommend making the code more general, instead of hardcoding it to 10 parts.
我将编写这段代码以便您学习该技术,但您不应该这样做。像
list
和set
这样的容器数据类型的要点是,您可以拥有任意内容,而不必为每个元素创建变量。所以,不要这样做
(
chunks
配方来自 Ned Batchelder 在链接问题中的回答。您不应该这样做的原因是修改locals
(或者实际上globals
或vars
)不是一个好的做法:它会导致难以确定的行为,并且可能会出现非常讨厌的错误。I'll write this code so you learn the technique, but you shouldn't do this. The point of container datatypes like
list
andset
is that you can have arbitrary contents without having to make variables for each elements. So,Don't do this
(The
chunks
recipe is from Ned Batchelder's answer in the linked question. The reason you shouldn't do this is that modifyinglocals
(or indeedglobals
orvars
) is not good practice: it causes hard-to-determine behaviour and possibly very nasty bugs.如果您不需要强制输出元素的连续片段,那么以下简单的代码片段将完成这项工作:
基本上,代码是根据模余数对元素进行分组。正因为如此,输出列表中的元素将不连续。例如,如果输入是
range(21)
,您会得到
希望它有帮助,而不是。
If you don't need to enforce contiguous pieces of output elements, then the following simple snippet will do the job:
Basically the code is grouping elements based on modulo residues. And because of exactly that, the elements in the output list will not be contiguous. For example, if the input is
range(21)
, instead ofyou would get
Hope it helps.
另一种选择
Another alternative
使用元组/列表结果 - 最合理的方法
如果您需要定义新变量,您可以
setattr
并向任何对象
添加新属性。这是安全的,因为您不会覆盖现有变量:locals()
或globals()
字典中。Use tuple/list a result - the most reasonable approach
If you need to define new variables, you can
setattr
and add new attributes to anyobject
. It is safe since you won't overwrite existing variables:locals()
orglobals()
dictionary depending on the context your code is running in.看到了几个解决方案,但无法帮助发布我的:
当然,可能可以进一步总结,但我认为这样读起来很清楚。
Seen several solutions, but couldn't help post mine:
Can probably be summarized further, of course, but I think this way it is clear to read.
与数据帧的 @henneray 相同
Same as @henneray for dataframes