从相同的`/ dev /`中选择开发者

时间:2018-11-09 11:02:59

标签: python-3.x

我想计算/dev中相同种类的设备

import os
from typing import List, Dict

devs: List[str] = os.listdir("/dev")

def counter(devs: List) -> Dict:
    count: Dict(str, List) = dict() 

    for dev in devs:
        key = dev[:2] #select the first two character as key
        if key not in count:
            count[key] =[] # initiate a list
            count[key].append(dev) 
        if key in count:
            count[key].append(dev)

    return count

它输出:

{'co': ['console', 'console'],
 'tt': ['tty',
  'tty',
  'ttyp0',
  'ttyp1',

观察代码,发现count[key].append(dev)的缩写,
我怎样才能尽可能简洁地重构它?

0 个答案:

没有答案