Append to a dict of lists with a dict comprehension

前端 未结 4 1205
你的背包
你的背包 2021-02-20 05:20

Suppose I have a large list of words. For an example:

>>> with open(\'/usr/share/dict/words\') as f:
...     words=[word for word in f.read().split(\'\\         


        
4条回答
  •  余生分开走
    2021-02-20 06:05

    No - dict comprehensions are designed to generate non-overlapping keys with each iteration; they don't support aggregation. For this particular use case, a loop is the proper way to accomplish the task efficiently (in linear time).

提交回复
热议问题