如何在Python中平均分割列表块?
我正在尝试使用热谱图将用户添加到我的组中 我的列表中有 200 个用户 id - python
list_of_users = [user_id1, user_id2, user_id3, user_id4, ...]
我也有一个 7 个客户端的列表,我要做的是分发,7 个客户端之间没有用户 id 列表(大约相等)并添加它们,而且我有时也有不均匀的情况用户数量那么我如何使用 python 分配列表并相应地添加用户?
顺便说一句:如果 2-3 个用户没有正确分配也没关系,就像我想分配大约。并添加它们,但任何用户都不应错过。
我尝试了这个函数 -
def divide_chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
但它分布不均匀,它分布特定数量的块,最后给出剩余的块,这不是我想要的。
简而言之:我希望自动决定输出并决定如何均匀分配用户 ID。
stackover flow 中的大多数答案我们必须决定我不想要的块的数量 - 我想做的就是将 x 数量的项目分配到 y 数量相等的部分
i am trying to add users to my group using pyrogram
i have 200 user ids in a list - python
list_of_users = [user_id1, user_id2, user_id3, user_id4, ...]
i also, have a list of 7 clients, what i waana do is distribute, no of list of user ids among 7 clients (approx. equally) and add them, also i sometimes have uneven number of users so how do i distribute the list add users accordingly using python?
btw : its okay if 2-3 users are not properly distributed, like i wanna distribute approx. and add them but none of the users should miss.
i tried this function -
def divide_chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
but it doesn't distribute evenly it distributes specific number of chuncks and at last gives remaining chunks which is not what i want.
inshort : i want the output to be autodecided and decide how to evenly distribute the user ids.
most of answer in stackover flow we have to decide no of chunks i don't wanna - all i want to do is distribute the x no of items into y no of equal parts
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用:
更多信息:文档
You can use:
More in: Docs
DIY:无需外部库
这是一种无需外部库的方法。如果可能的话,此实现将为每个客户端分配相同数量的用户。如果不是,它将确保分配给客户端的用户数量差异最大为 1(= 我对
公平
的定义)。此外,如果您要多次运行此命令,它将确保不会始终将其他用户分配给相同的客户端。它通过随机选择一组客户端来实现这一点,这些客户端将需要承担剩余用户之一(无法以相等的比例分配给客户端)。这确保了用户向客户端的公平分配。我发布的代码有点多,因此这里有一些高级解释:
相关函数称为
assign_users_to_clients()
。这将完成您想做的工作。另外两个函数verify_all_users_assigned()
和print_mapping()
只是本演示中的实用函数。一个将确保分配正确,即用户被准确地分配给一个客户端(没有重复的分配,没有未分配的用户),另一个只是打印更好一点的结果,以便您可以验证用户到客户端的分配实际上是公平的。预期产出
使用外部库
时外部库有很多选择。请参阅例如函数
pandas.cut()
< /a> 或numpy.split()
。当不可能将用户公平分配给客户端时,他们的行为会有所不同,因此您应该阅读文档中的内容。DIY: Without external libraries
Here is one approach without external libraries. This implementation will assign an equal number of users to each client if possible. If not it will make sure the difference in number of users assigned to clients between clients is at max 1 (= my definition of
fair
). Additionally, it will make sure that additional users are not assigned to the same clients all the time, if you were to run this multiple times. It does this by randomly choosing the set of clients which will need to take on one of the remaining users (that could not be assigned to clients in equal parts). This ensures a fair allocation of users to clients.It's a bit more code that I post, so here some high-level explanation:
The relevant function is called
assign_users_to_clients()
. This will do the job you intend to do. The two other functionsverify_all_users_assigned()
andprint_mapping()
are just utility functions for the sake of this demo. One will make sure the assignment is correct, i. e. users are assigned to exactly one client (no duplicate assignments, no unassigned users) and the other just prints the result a bit nicer so you can verify that the distribution of users to clients is actually fair.Expected output
With external libraries
When using external libraries there are many options. See e.g. function
pandas.cut()
ornumpy.split()
. They will act differently when a fair distribution of users to clients is not possible so you should read on that in the documentation.