Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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中的字典函数出现错误_Python_Dictionary - Fatal编程技术网

Python中的字典函数出现错误

Python中的字典函数出现错误,python,dictionary,Python,Dictionary,我的python脚本的一部分: (我首先制作了字典“h”) def直方图(L): d={} 对于L中的x: 如果x在d中: d[x]+=1 其他: d[x]=1 返回d h=直方图(列表) 对于vhfile中的vhfile: linelist=commands.getoutput('cat'+vhfile).splitlines(True) 列表1=[] 对于行列表中的行: name1=line.split()[0] 如果int(h[name1])当您试图在dict中查找某个内容时,会出现key

我的python脚本的一部分: (我首先制作了字典“h”)

def直方图(L):
d={}
对于L中的x:
如果x在d中:
d[x]+=1
其他:
d[x]=1
返回d
h=直方图(列表)
对于vhfile中的vhfile:
linelist=commands.getoutput('cat'+vhfile).splitlines(True)
列表1=[]
对于行列表中的行:
name1=line.split()[0]

如果int(h[name1])当您试图在
dict
中查找某个内容时,会出现
keyrorm
,而
dict
中不包含该键


在这种情况下,键
'080821\u HWI-EAS301\u 0002\u 30albaxx:1:46:1643:1310'
似乎没有出现在
h
中,当您试图在
dict
中查找某个内容时,会出现
键错误,并且
dict
不包含该键


在这种情况下,键
'080821\u HWI-EAS301\u 0002\u 30ALBAXX:1:46:1643:1310'
似乎没有出现在
h
中。键错误表示您引用了dict中不存在的键。检索指定键处的值时出错,因为该键不存在

处理此问题的一种方法是使用try/except块。如果“try”中的代码引发了“KeyError”,您就知道name1不在h中,您可以做任何适当的事情

for line in linelist:
    name1 = line.split()[0]
    try:
        if int(h[name1]) <= 300:
           list1.append(line)
    except KeyError:
         <code here to deal with the condition>
在2.7之前,您可以使用defaultdict获得相同的结果:

>>> from collections import defaultdict
>>> dd = defaultdict(int)
>>> for word in LIST:
...     dd[word] += 1
... 
>>> dd
defaultdict(<type 'int'>, {'defaultdict,': 1, 'elements.': 1, "don't": 1, 'is': 1, 'at': 1, 'need': 1, 'sentence': 1, 'split': 1, 'get': 2, 'you': 1, 'into': 1, 'function': 1, 'elements': 1, 'multiple': 1, 'that': 1, 'This': 1, 'histogram': 1, 'using': 1, 'The': 1, 'a': 1, 'all.': 1, 'list': 2, 'will': 2, 'so': 1, 'the': 1, 'counted': 1})
>>从集合导入defaultdict
>>>dd=默认dict(int)
>>>对于列表中的单词:
...     dd[word]+=1
... 
>>>dd
defaultdict(,{'defaultdict':1,'elements':1,“不”:1,'is':1,'at':1,'need':1,'Session':1,'split':1,'get':2,'you':1,'into':1,'function':1,'elements':1,'multiple':1,'that':1,'This':1,'histogram':1,'using':1,'using':1,'s':1,'s':1,'list''s''s':2,'

密钥错误表示您引用了dict中不存在的密钥。检索指定键处的值时出错,因为该键不存在

处理此问题的一种方法是使用try/except块。如果“try”中的代码引发了“KeyError”,您就知道name1不在h中,您可以做任何适当的事情

for line in linelist:
    name1 = line.split()[0]
    try:
        if int(h[name1]) <= 300:
           list1.append(line)
    except KeyError:
         <code here to deal with the condition>
在2.7之前,您可以使用defaultdict获得相同的结果:

>>> from collections import defaultdict
>>> dd = defaultdict(int)
>>> for word in LIST:
...     dd[word] += 1
... 
>>> dd
defaultdict(<type 'int'>, {'defaultdict,': 1, 'elements.': 1, "don't": 1, 'is': 1, 'at': 1, 'need': 1, 'sentence': 1, 'split': 1, 'get': 2, 'you': 1, 'into': 1, 'function': 1, 'elements': 1, 'multiple': 1, 'that': 1, 'This': 1, 'histogram': 1, 'using': 1, 'The': 1, 'a': 1, 'all.': 1, 'list': 2, 'will': 2, 'so': 1, 'the': 1, 'counted': 1})
>>从集合导入defaultdict
>>>dd=默认dict(int)
>>>对于列表中的单词:
...     dd[word]+=1
... 
>>>dd
defaultdict(,{'defaultdict':1,'elements':1,“不”:1,'is':1,'at':1,'need':1,'Session':1,'split':1,'get':2,'you':1,'into':1,'function':1,'elements':1,'multiple':1,'that':1,'This':1,'histogram':1,'using':1,'using':1,'s':1,'s':1,'list''s''s':2,'

您可以使用
集合
标准模块中的
计数器
类更轻松地构建直方图。您可以使用
集合
标准模块中的
计数器
类更轻松地构建直方图。
>>> LIST = "This is a sentence that will get split into multiple list elements. The list elements will get counted using defaultdict, so you don't need the histogram function at all.".split()    
>>> LIST
['This', 'is', 'a', 'sentence', 'that', 'will', 'get', 'split', 'into', 'multiple', 'list', 'elements.', 'The', 'list', 'elements', 'will', 'get', 'counted', 'using', 'defaultdict,', 'so', 'you', "don't", 'need', 'the', 'histogram', 'function', 'at', 'all.']    
>>> from collections import Counter    
>>> c = Counter(LIST)
>>> c
Counter({'get': 2, 'list': 2, 'will': 2, 'defaultdict,': 1, 'elements.': 1, "don't": 1, 'is': 1, 'at': 1, 'need': 1, 'sentence': 1, 'split': 1, 'you': 1, 'into': 1, 'function': 1, 'elements': 1, 'multiple': 1, 'that': 1, 'This': 1, 'histogram': 1, 'using': 1, 'The': 1, 'a': 1, 'all.': 1, 'so': 1, 'the': 1, 'counted': 1})
>>> from collections import defaultdict
>>> dd = defaultdict(int)
>>> for word in LIST:
...     dd[word] += 1
... 
>>> dd
defaultdict(<type 'int'>, {'defaultdict,': 1, 'elements.': 1, "don't": 1, 'is': 1, 'at': 1, 'need': 1, 'sentence': 1, 'split': 1, 'get': 2, 'you': 1, 'into': 1, 'function': 1, 'elements': 1, 'multiple': 1, 'that': 1, 'This': 1, 'histogram': 1, 'using': 1, 'The': 1, 'a': 1, 'all.': 1, 'list': 2, 'will': 2, 'so': 1, 'the': 1, 'counted': 1})