I have a list of names, e.g. [\'Agrajag\', \'Colin\', \'Deep Thought\', ... , \'Zaphod Beeblebrox\', \'Zarquon\']
. Now I want to partition this list into approx
Since your name_list
has to be sorted for groupby
to work, can't you just check every Nth value and build your divisions that way?
right_endpoints = name_list[N-1::N]
And using "A"
as your leftmost endpoint and "Z"
as your rightmost endpoint, you can construct the N divisions accordingly and they should all have the same size.
right_endpoints[0]
.right_endpoints[0]
, the next right endpoint would be right_endpoints[1]
.The issue you may run into is what if two of these right_endpoints
are the same...
edit: example
>>> names = ['Aaron', 'Abel', 'Cain', 'Daniel', 'Darius', 'David', 'Ellen', 'Gary', 'James', 'Jared', 'John', 'Joseph', 'Lawrence', 'Michael', 'Nicholas', 'Terry', 'Victor', 'Zulu']
>>> right_ends, left_ends = names[2::3], names[3::3]
>>> left_ends = ['A'] + left_ends
>>> left_ends, right_ends
>>> ["%s - %s" % (left, right) for left, right in zip(left_ends, right_ends)]
['A - Cain', 'Daniel - David', 'Ellen - James', 'Jared - Joseph', 'Lawrence - Nicholas', 'Terry - Zulu']