[英]Splitting list into a dictionary of lists by a custom key function
I implemented a small helper function that splits a list into a dictionary by a custom key function.我实现了一个小助手 function,它通过自定义键 function 将列表拆分为字典。 The following implementation works, but feels rather un-pythonic:
以下实现有效,但感觉相当非 Pythonic:
def classify_list_by_key(l, key_func):
result = defaultdict(list)
for item in l:
result[key_func(item)].append(item)
return result
I tried using the following dict comprehension, which obviously has horrible performance with a lot of unnecessary calls to key_func
and quadratic run-time.我尝试使用以下 dict 理解,这显然具有可怕的性能,对
key_func
和二次运行时进行了很多不必要的调用。
def classify_list_by_key_dict_comprehension(l, key_func):
return {key_func(x): [y for y in objects if key_func(x) == key_func(y)] for x in l}
Sample in- and output:样品输入和 output:
str_list = "these are some test strings for testing classify_list_by_key function".split()
print(classify_list_by_key(str_list, len))
produces生产
defaultdict(<class 'list'>, {5: ['these'], 3: ['are', 'for'], 4: ['some', 'test'], 7: ['strings', 'testing'], 20: ['classify_list_by_key'], 8: ['function']})
Is there a better / more pythonic way to achieve this using built-ins or standard library modules?有没有更好/更pythonic的方法来使用内置或标准库模块来实现这一点?
It is possible using groupby
from itertools
, but it requires a sorted list for that purpose.可以使用
itertools
中的groupby
,但为此需要一个排序列表。
Here is the code for that:这是代码:
from itertools import groupby
str_list = "these are some test strings for testing classify_list_by_key function".split()
str_list.sort(key=len)
res = {k: list(g) for k, g in groupby(str_list, key=len)}
print(res)
Output Output
{3: ['are', 'for'], 4: ['some', 'test'], 5: ['these'], 7: ['strings', 'testing'], 8: ['function'], 20: ['classify_list_by_key']}
The following is an alternative implementation which does not force to use a defaultdict:以下是不强制使用 defaultdict 的替代实现:
def classify_list_by_key(l, key_func):
result = {}
for item in l:
result.setdefault(key_func(item), []).append(item)
return result
str_list = "these are some test strings for testing classify_list_by_key function".split()
print(classify_list_by_key(str_list, len))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.