+ 1
How to add an element to dictionary?
except this form a={'c':2} a ['b']=3 is there any other form to add?
5 Answers
+ 1
https://stackoverflow.com/q/1024847/8458437
There appears to be an update method on dictionaries
+ 1
Yea Thankuš..but I don't wanna update as it inserts a new value for the same key, if the key already exists. I wanted to insert another key value pair even if the key has already occurred.
hope u got my pointš
+ 1
hm not sure that's possible, the point of a dictionary is having a unique key to map to a value, but values need not be unique. Is your dictionary set up correctly?
+ 1
Actually now I have modified my code a bit
sent=[['this','is','beautiful','and','nice','place'],['nice','place']]
adj=['beautiful','nice']
freq={}
a=1
for c in sent:
for x in c:
if x in adj:
freq [a]={}
freq [a][x]=c.count (x)
a+=1
print (freq)
the output I'm getting is
{1:{'nice':1},2:{'nice':1}}
bt i need the following output,
{1:{'beautiful':1},{'nice':1},2:{'nice':1}}
0
not that i know off