Question

I am trying to create a list of lists based on hashes. That is, I want a list of lists of items that hash the same. Is this possible in a single-line comprehension?

Here is the simple code that works without comprehensions:

def list_of_lists(items):
    items_by_hash = defaultdict(list)
    for item in items:
        words_by_key[hash(item)].append(item)
    return words_by_key.values()

For example, let's say we have this simple hash function:

def hash(string):
    import __builtin__
    return __builtin__.hash(string) % 10

Then,

>>> l = ['sam', 'nick', 'nathan', 'mike']
>>> [hash(x) for x in l]
[4, 3, 2, 2]
>>>
>>> list_of_lists(l)
[['nathan', 'mike'], ['nick'], ['sam']] 

Is there any way I could do this in a comprehension? I need to be able to reference the dictionary I'm building mid-comprehension, in order to append the next item to the list-value.

This is the best I've got, but it doesn't work:

>>> { hash(word) : [word] for word in l }.values()
[['mike'], ['nick'], ['sam']]

It obviously creates a new list every time which is not what I want. I want something like

{ hash(word) : __this__[hash(word)] + [word] for word in l }.values()

or

>>> dict([ (hash(word), word) for word in l ])
{2: 'mike', 3: 'nick', 4: 'sam'}

but this causes the same problem.

Was it helpful?

Solution

[[y[1] for y in x[1]] for x in itertools.groupby(sorted((hash(y), y)
  for y in items), operator.itemgetter(0))]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top