我想计算/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)
的缩写,
我怎样才能尽可能简洁地重构它?