from collections import defaultdict

def group_by(lst, fn):
  d = defaultdict(list)
  for el in lst:
    d[fn(el)].append(el)
  return dict(d)
from math import floor

group_by([6.1, 4.2, 6.3], floor) # {4: [4.2], 6: [6.1, 6.3]}
group_by(['one', 'two', 'three'], len) # {3: ['one', 'two'], 5: ['three']}

 

相关文章:

  • 2022-02-14
  • 2022-02-07
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-28
  • 2022-02-24
  • 2021-08-01
  • 2022-12-23
  • 2021-10-25
  • 2022-01-10
  • 2021-10-28
相关资源
相似解决方案