determining which value is the first to appear five times in a list?

Terry Reedy tjreedy at udel.edu
Sat Feb 6 14:42:52 EST 2010


On 2/6/2010 2:09 PM, Wolodja Wentland wrote:
> On Sat, Feb 06, 2010 at 13:24 -0500, Chris Colbert wrote:
>> [(match_val_291, identifier_b), (match_val_23, identifier_b), (match_val_22,
>> identifer_k) .... ]
>>
>> Now, what I would like to do is step through this list and find the identifier
>> which appears first a K number of times.
>
> I think you can use the itertools.groupby(L, lambda el: el[1]) to group
> elements in your *sorted* list L by the value el[1] (i.e. the
> identifier) and then iterate through these groups until you find the
> desired number of instances grouped by the same identifier.

This will generally not return the same result. It depends on whether OP 
wants *any* item appearing at least 5 times or whether the order is 
significant and the OP literally wants the first. Sorting the entire 
list may also take a *lot* longer.

Terry Jan Reedy




More information about the Python-list mailing list