如何在词典理解中使用+=运算符

2024-09-29 18:36:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我能够通过以下方式建立字数词典:

dicc = {}
for word in splitt:
    if word not in dicc:
        dicc[word]=0
    if word in dicc:
        dicc[word]+=1 

我正试着用字典来理解它。 以下声明有效:

countDicc = {word:(0 if word not in countDicc else +1) for word in splitt}

显然,我需要+=1让字典发挥作用,然而,这句话是无效的;如何在字典理解中实现+=


Tags: in声明forif字典方式notelse

热门问题