Reputation: 1049
I read data from json with this code .
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()
it show data like this.
<class 'list'>
aa
bb
aa
cc
bb
cc
aa
I want to count frequentcy of word the result should be aa=3, bb=2, cc=2
If I uncomment at Counter(data['behavior']['processes'][3]['calls'])
it will show error.
TypeError: unhashable type: 'dict'
How to count word from list ?
Upvotes: 0
Views: 98
Reputation: 2047
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)
Upvotes: 1
Reputation: 20339
You can do
Counter(map(lambda x:x['argument'], data['behavior']['processes'][3]['calls']))
Upvotes: 1
Reputation: 2962
Haven't tested it since i dont have the data you're using.
But i think this would work.
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)
Upvotes: 0
Reputation: 3852
Counter requires a list as input.
from collections import Counter
#create a list from your data
mylist = [i['arguments'] for i in data['behavior']['processes'][3]['calls']]
#make a dict of counts
counter_dict = Counter(mylist)
#print out counts per item
for val in counter_dict:
print '%i has %i occurrences' % (val, counter_dict[val])
(code untested)
Upvotes: 0