Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 添加+;1如果字典中存在部分查询_Python - Fatal编程技术网

Python 添加+;1如果字典中存在部分查询

Python 添加+;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

我想检查字典键中是否存在部分字符串,如果存在,则添加值+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 '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
可能不是一个选项。