Python 添加+;1如果字典中存在部分查询
我想检查字典键中是否存在部分字符串,如果存在,则添加值+1Python 添加+;1如果字典中存在部分查询,python,Python,我想检查字典键中是否存在部分字符串,如果存在,则添加值+1 dic = { 'partial sentence':2, 'complete sentence':1 } print(any('partial' in sentence for sentence in dic)) 此时,我可以说“partial”存在于“partial-station”中,但是,如何向值添加+1?您需要在迭代dict时显式执行此操作 for sentence in dic: if 'part
dic = {
'partial sentence':2,
'complete sentence':1
}
print(any('partial' in sentence for sentence in dic))
此时,我可以说“partial”存在于“partial-station”中,但是,如何向值添加+1?您需要在迭代dict时显式执行此操作
for sentence in dic:
if 'partial' in sentence:
dic[sentence] += 1
词典理解。在蟒蛇3中 [6]中的
:newdict={k:v+1如果k中的'partial'表示k,则v表示k,v表示dic.items()}
在[7]中:newdict
Out[7]:{‘部分句子’:3,‘完整句子’:1}
在[8]中:
dic[句子]+=1Oh,右;有三个键
的实例我想用句子
:)创建一个新的dict
可能不是一个选项。