python - Iterating and modifying a dictionary -
I have a code in which the dictionary of the words is a dictionary, the meaning of the program is to merge the words.
It is that if the word is the word
in the word i , then the merge the value of the word [word i]
Remove dictionary [word i]
for i in word [word j]
and my_dict.keys (): my_dict in Jammu .keys (): if i! = J: if i in j: my_dict [j] + = my_dict [i] del my_dict [i] elif j in: my_dict [i] + = my_dict [j] del my_dict [j]
Is this code valid?
Your code is not valid Do not place any command in Python to remove elements from the dictionary Delete
is called del
You can delete an item from the dictionary like this:
del your_dictionary ['some_key']
Although this is not a great python you can refactor your code like this:
for i_dict.keys () in: for j my_dict .keys (): if i! = J: if i in j: my_dict [j] = = my_dict.pop (i) in alif h i: my_dict [i] + = my_dict.pop (j)
Pop>
is a dictionary method, which returns and removes an item from the dictionary.
In the above code dictionary, there is a suggestion regarding the merge of closing values (closing) for the double for loop still looks ugly but first work it, Can improve quality. I hope my example can help you.
Comments
Post a Comment