Python:将名称列表划分为大小相等的子列表 [英] Python: partition list of names into equally sized sublists
问题描述
我有一个名字列表,例如['Agrajag', 'Colin', 'Deep Thought', ... , 'Zaphod Beeblebrox', 'Zarquon']
.现在,我想将此列表划分为大小大致相等的子列表,以使子组的边界位于名称的首字母,例如AF,GL,MP,QZ,而不是A-Fe,Fi-Mo,Mu-Pra ,Z之前.
I have a list of names, e.g. ['Agrajag', 'Colin', 'Deep Thought', ... , 'Zaphod Beeblebrox', 'Zarquon']
. Now I want to partition this list into approximately equally sized sublists, so that the boundaries of the subgroups are at the first letter of the names, e.g A-F, G-L, M-P, Q-Z, not A-Fe, Fi-Mo, Mu-Pra, Pre-Z.
我只能提出一个静态大小的分区,而不考虑子组的大小:
I could only come up with a statically sized parition that doesn't take size of the subgroups into account:
import string, itertools
def _group_by_alphabet_key(elem):
char = elem[0].upper()
i = string.ascii_uppercase.index(char)
if i > 19:
to_c = string.ascii_uppercase[-1];
from_c = string.ascii_uppercase[20]
else:
from_c = string.ascii_uppercase[i/5*5]
to_c = string.ascii_uppercase[i/5*5 + 4]
return "%s - %s" % (from_c, to_c)
subgroups = itertools.groupby(name_list, _group_by_alphabet_key)
还有更好的主意吗?
P.S .:这听起来有点像作业,但实际上是针对网页的,其中成员应显示在5-10个相同大小的组的标签中.
P.S.: this may sound somewhat like homework, but it actually is for a webpage where members should be displayed in 5-10 tabs of equally sized groups.
推荐答案
这可能有效.我确信有一种更简单的方法……可能涉及到itertools
.请注意,num_pages
仅大致确定您实际将获得的页面数.
Here's something that might work. I feel certain there's a simpler way though... probably involving itertools
. Note that num_pages
only roughly determines how many pages you'll actually get.
糟糕!有一个错误-它切断了最后一组!下面的内容应该是固定的,但是请注意,最后一页的长度会有些不可预测.另外,我添加了.upper()
来考虑可能的小写字母名称.
Whoops! There was a bug -- it was cutting off the last group! The below should be fixed, but note that the length of the last page will be slightly unpredictable. Also, I added .upper()
to account for possible lowercase names.
以前定义letter_groups的方法效率很低;以下基于dict的代码更具可扩展性:
The previous method of defining letter_groups was inefficient; the below dict-based code is more scalable:
names = ['Agrajag', 'Colin', 'Deep Thought', 'Ford Prefect' , 'Zaphod Beeblebrox', 'Zarquon']
num_pages = 3
def group_names(names, num_pages):
letter_groups = defaultdict(list)
for name in names: letter_groups[name[0].upper()].append(name)
letter_groups = [letter_groups[key] for key in sorted(letter_groups.keys())]
current_group = []
page_groups = []
group_size = len(names) / num_pages
for group in letter_groups:
current_group.extend(group)
if len(current_group) > group_size:
page_groups.append(current_group)
current_group = []
if current_group: page_groups.append(current_group)
return page_groups
print group_names(names, num_pages)
这篇关于Python:将名称列表划分为大小相等的子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!