如何统计列表中的单词?

时间:2017-05-03 18:01:06

标签: python list python-3.x

我用这段代码从json读取数据。

json_file='report.json'

json_data=open(json_file)
data = json.load(json_data)

t0 = []
t1 = []
tn = []

#counts = Counter(data['behavior']['processes'][3]['calls'])
print (type(data['behavior']['processes'][3]['calls']))

for i in data['behavior']['processes'][3]['calls']:

    t0 = i['arguments']
    print(t0)

json_data.close()

它显示这样的数据。

<class 'list'>
aa
bb
aa
cc
bb
cc
aa

我想计算词的频率,结果应该是aa = 3,bb = 2,cc = 2

如果我在Counter(data['behavior']['processes'][3]['calls'])取消注释,则会显示错误。

TypeError: unhashable type: 'dict'

如何统计列表中的单词?

4 个答案:

答案 0 :(得分:1)

你可以做到

Counter(map(lambda x:x['argument'], data['behavior']['processes'][3]['calls']))

答案 1 :(得分:1)

counterDict = {}   # <==
json_file='report.json'
json_data=open(json_file)
data = json.load(json_data)

t0 = []
t1 = []
tn = []

#counts = Counter(data['behavior']['processes'][3]['calls'])
print (type(data['behavior']['processes'][3]['calls']))

for i in data['behavior']['processes'][3]['calls']:

    t0 = i['arguments']
    counterDict[t0] = counterDict.get(t0,0)+1 # <===

json_data.close()

print(counterDict)

答案 2 :(得分:0)

计数器需要一个列表作为输入。

No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:8080' is therefore not allowed access.

(代码未经测试)

答案 3 :(得分:0)

由于我没有您正在使用的数据,因此Haven未对其进行测试 但我认为这会奏效。

json_file='report.json'

json_data=open(json_file)
data = json.load(json_data)

t0 = []
t1 = []
tn = []

#counts = Counter(data['behavior']['processes'][3]['calls'])
print (type(data['behavior']['processes'][3]['calls']))

data_count = {}
for i in data['behavior']['processes'][3]['calls']:

    t0 = i['arguments']
    count = data_count.get(t0)
    if count is None:
        data_count[t0] = 1
    else:
        data_count[t0] = count + 1

    print(t0)

json_data.close()
print(data_count)