Re: [Tutor] deleting elements of a dictionary (Mats Wichmann)
Having now done a quick check, mydict.pop() is no better for this case. Here's a simplistic sample that does work: d = { 100:3, 200:4, 111:5, 222:5, 333:5, 500:6, } print "original: ", d new = {key:value for (key,value) in d.iteritems() if value != 5} print "new: ", new === """ In this case, you ned to specify by hand the entry you want to delete. In case you want to do it without looking at the stuff, here is one way paraphrasing the stackoverflow post https://stackoverflow.com/questions/20672238/find-dictionary-keys-with-duplicate-values """ # d = { 100:3, 200:4, 111:5, 222:5, 333:5, 500:6, 50:7, 60:7, } print ("original: ", d) rev_multidict = {} for key, value in d.items(): rev_multidict.setdefault(value, set()).add(key) print(rev_multidict) v = [values for key, values in rev_multidict.items() if len(values) > 1] print('\t Set of keys with same value ', v) #[print(i) for element in v for i in list(element)] [d.pop(i) for element in v for i in list(element)] print('d with every repeated stuff deleted: ', d) Sergio Enhance your #MachineLearning and #BigData skills via #Python #SciPy 1) https://www.packtpub.com/big-data-and-business-intelligence/numerical-and-scientific-computing-scipy-video 2) https://www.amazon.com/Learning-Numerical-Scientific-Computing-Second/dp/1783987707/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
On 05/19/2017 02:54 PM, Mats Wichmann wrote: > On 05/19/2017 11:17 AM, Alan Gauld via Tutor wrote: >> On 19/05/17 15:23, Michael C wrote: >>> list(read_dictionary) converts the dictionary into a list right? How can >>> you save the list as a dictionary? >> >> Nope, list() produces a new list object containing the >> keys of the dictionary. In the old day(of python 2) you >> used to get the same effect using >> >> for key in mydict.keys() >> >> but keys() now returns a funky view of the original dict >> keys and I suspect you'd have the same problems when >> deleting items. So Peter's list() is probably the best >> option. >> > > Or to take another related view: > > don't remove items from an iterable while iterating over it: del() is > okay as long as you're not looping over the thing. > > Dictionaries have a method for this called pop(), but to my blushes, I > don't really have a lot of experience with it. > > What I'd think of just off the bat is build a new dictionary on the fly, > omitting the things you were trying to delete, and then if you like, > save the new dict by the old name (which will cause the old one to have > no references and be dropped. Having now done a quick check, mydict.pop() is no better for this case. Here's a simplistic sample that does work: d = { 100:3, 200:4, 111:5, 222:5, 333:5, 500:6, } print "original: ", d new = {key:value for (key,value) in d.iteritems() if value != 5} print "new: ", new ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
On 05/19/2017 11:17 AM, Alan Gauld via Tutor wrote: > On 19/05/17 15:23, Michael C wrote: >> list(read_dictionary) converts the dictionary into a list right? How can >> you save the list as a dictionary? > > Nope, list() produces a new list object containing the > keys of the dictionary. In the old day(of python 2) you > used to get the same effect using > > for key in mydict.keys() > > but keys() now returns a funky view of the original dict > keys and I suspect you'd have the same problems when > deleting items. So Peter's list() is probably the best > option. > Or to take another related view: don't remove items from an iterable while iterating over it: del() is okay as long as you're not looping over the thing. Dictionaries have a method for this called pop(), but to my blushes, I don't really have a lot of experience with it. What I'd think of just off the bat is build a new dictionary on the fly, omitting the things you were trying to delete, and then if you like, save the new dict by the old name (which will cause the old one to have no references and be dropped. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
On 19/05/17 15:23, Michael C wrote: > list(read_dictionary) converts the dictionary into a list right? How can > you save the list as a dictionary? Nope, list() produces a new list object containing the keys of the dictionary. In the old day(of python 2) you used to get the same effect using for key in mydict.keys() but keys() now returns a funky view of the original dict keys and I suspect you'd have the same problems when deleting items. So Peter's list() is probably the best option. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
list(read_dictionary) converts the dictionary into a list right? How can you save the list as a dictionary? Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
Michael C wrote: > for n in list(read_dictionary): >> print(read_dictionary[n]) >> if read_dictionary[n] == '5': >> del read_dictionary[n] > > After doing this how do I save it back to the dictionary? > then i ll do this > numpy.save('loc_string_dictionary.npy', dictionary) Hm, the dict is called `read_dictionary`, so you save it with numpy.save('loc_string_dictionary.npy', read_dictionary) but I may have understood the question. If so, please clarify. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
for n in list(read_dictionary): > print(read_dictionary[n]) > if read_dictionary[n] == '5': > del read_dictionary[n] After doing this how do I save it back to the dictionary? then i ll do this numpy.save('loc_string_dictionary.npy', dictionary) On Thu, May 18, 2017 at 3:05 PM, Peter Otten <__pete...@web.de> wrote: > Michael C wrote: > > > I am trying to remove incorrect entries of my dictionary. > > I have multiple keys for the same value, > > > > ex, > > [111]:[5] > > [222]:[5] > > [333]:[5} > > > > and I have found out that some key:value pairs are incorrect, and the > best > > thing to do > > is to delete all entries who value is 5. So this is what I am doing: > > > > import numpy > > read_dictionary = numpy.load('loc_string_dictionary.npy').item() > > > > for n in read_dictionary: > > print(read_dictionary[n]) > > if read_dictionary[n] == '5': > > del read_dictionary[n] > > > > > > However, I get this error: > > RuntimeError: dictionary changed size during iteration > > > > and I can see why. > > > > What's the better thing to do? > > You can copy the keys into a list: > > for n in list(read_dictionary): > > print(read_dictionary[n]) > > if read_dictionary[n] == '5': > > del read_dictionary[n] > > As the list doesn't change size during iteration there'll be no error or > skipped key aka list item. > > If there are only a few items to delete build a list of keys and delete the > dict entries in a secend pass: > > delenda = [k for k, v in read_dictionary.items() if v == "5"] > for k in delenda: > del read_dictionary[k] > > If there are many items to delete or you just want to default to the most > idiomatic solution put the pairs you want to keep into a new dict: > > read_dictionary = {k: v for k, v in read_dictionary.items() if v != "5"} > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
Michael C wrote: > I am trying to remove incorrect entries of my dictionary. > I have multiple keys for the same value, > > ex, > [111]:[5] > [222]:[5] > [333]:[5} > > and I have found out that some key:value pairs are incorrect, and the best > thing to do > is to delete all entries who value is 5. So this is what I am doing: > > import numpy > read_dictionary = numpy.load('loc_string_dictionary.npy').item() > > for n in read_dictionary: > print(read_dictionary[n]) > if read_dictionary[n] == '5': > del read_dictionary[n] > > > However, I get this error: > RuntimeError: dictionary changed size during iteration > > and I can see why. > > What's the better thing to do? You can copy the keys into a list: for n in list(read_dictionary): > print(read_dictionary[n]) > if read_dictionary[n] == '5': > del read_dictionary[n] As the list doesn't change size during iteration there'll be no error or skipped key aka list item. If there are only a few items to delete build a list of keys and delete the dict entries in a secend pass: delenda = [k for k, v in read_dictionary.items() if v == "5"] for k in delenda: del read_dictionary[k] If there are many items to delete or you just want to default to the most idiomatic solution put the pairs you want to keep into a new dict: read_dictionary = {k: v for k, v in read_dictionary.items() if v != "5"} ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] deleting elements of a dictionary
I am trying to remove incorrect entries of my dictionary. I have multiple keys for the same value, ex, [111]:[5] [222]:[5] [333]:[5} and I have found out that some key:value pairs are incorrect, and the best thing to do is to delete all entries who value is 5. So this is what I am doing: import numpy read_dictionary = numpy.load('loc_string_dictionary.npy').item() for n in read_dictionary: print(read_dictionary[n]) if read_dictionary[n] == '5': del read_dictionary[n] However, I get this error: RuntimeError: dictionary changed size during iteration and I can see why. What's the better thing to do? thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor