dictionary help
Hi, kind of a newbie here, but I have two questions that are probably pretty simple. 1. I need to get rid of duplicate values that are associated with different keys in a dictionary. For example I have the following code. s={} s[0]=[10,2,3] s[10]=[22,23,24] s[20]=[45,5] s[30]=[2,4] s[40]=[6,7,8] Now I want to be able to loop through the primary keys and get rid of duplicates (both in keys and values) so that I would have either a new dictionary or the same dictionary but with the following values: s[0]=[3] s[10]=[22,23,24] s[20]=[45,5] s[30]=[2,4] s[40]=[6,7,8] It doesn't matter which value gets removed as long as there is only one remaining, so in this example it doesn't matter that 2 got removed from s[0] or from s[30] as long as there is only one 2 in the dictionary. 2. I need to be able to loop over the values in the dictionary when there are multiple values assigned to each key like above and assign new values to those values. Taking the above example I would want to assign a new value so that when you called s[0] it would equal [3,4] say if 4 was the new value. I think this should be as simple as adding a value, but I kept on having difficulty. Any suggestions would be greatly appreciated. Thank you very much, Krishna -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary help
Thanks for the help. Actually this is part of a much larger project, but I have unfortunately pigeon-holed myself into needing to do these things without a whole lot of flexibility. To give a specific example I have the following dictionary where I need to remove values that are duplicated with other values and remove values that are duplicates of the keys, but still retain it as a dictionary. Each value is itself a class with many attributes that I need to call later on in the program, but I cannot have duplicates because it would mess up some estimation part of my model. d = {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]} So I want a new dictionary that would get rid of the duplicate values of 21, 22, 36 and 20 and give me back a dictionary that looked like this: new_d= {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12, 32], 26: [25, 27, 16], 30: [40]} I understand that a dictionary may not be the best approach, but like I said I have sort of pigeon-holed myself by the way that I am simulating my data and the estimation model that I am using. Any suggestions or comments about the above problem would be greatly appreciated. Thanks again, Krishna Dave Angel da...@ieee.org 08/11/09 7:38 AM Krishna Pacifici wrote: Hi, kind of a newbie here, but I have two questions that are probably pretty simple. 1. I need to get rid of duplicate values that are associated with different keys in a dictionary. For example I have the following code. s={} s[0]=[10,2,3] s[10]=[22,23,24] s[20]=[45,5] s[30]=[2,4] s[40]=[6,7,8] Now I want to be able to loop through the primary keys and get rid of duplicates (both in keys and values) so that I would have either a new dictionary or the same dictionary but with the following values: s[0]=[3] s[10]=[22,23,24] s[20]=[45,5] s[30]=[2,4] s[40]=[6,7,8] It doesn't matter which value gets removed as long as there is only one remaining, so in this example it doesn't matter that 2 got removed from s[0] or from s[30] as long as there is only one 2 in the dictionary. 2. I need to be able to loop over the values in the dictionary when there are multiple values assigned to each key like above and assign new values to those values. Taking the above example I would want to assign a new value so that when you called s[0] it would equal [3,4] say if 4 was the new value. I think this should be as simple as adding a value, but I kept on having difficulty. Any suggestions would be greatly appreciated. Thank you very much, Krishna Sounds like homework. If it was for an unconstrained project, I'd design a different data structure, one that directly enforced the data constraints. So far, I can't imagine a useful reason for this particular set of constraints. Let's break the problems down. 1a) Do you know how to write a loop that visits all the keys of a dictionary? 1b) Do you know how to safely check if a particular key exists? e.g. if key in s: 1c) Do you know how to collect a set of values, so that when a duplicate is found, it can be recognized as such? 1d) Do you know how to remove an item from a list? 2a) Like 1a) 2b) Do you know how to append a value to the end of a list? Is s[key] a list? DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary help
Wow, thanks MRAB and Simon, you guys are good. I guess I will go ahead and ask the next question that has also stumped me for awhile now. So basically I need to loop through the values in the new dictionary and append attributes of a class object. Each of the values (and keys) represent a block in a grid with a specific location (e.g. 35 is the block in row 3 col 5) and each block is an object with several attributes associated with it. I want to call that block and append two separate attributes to the dictionary within that same key. So again it would look something like this: block 35 has 2 attributes, say a and b, a=2, b=5 block 37 has a=1, b=3 block 46 has a=3, b=8 the two attributes come from two different definitions within the class statement, def detections ... return a def abundance ... return b so I would want to append to key 36 those two attributes for each block so that the resulting dictionary item would look like this: a b {36:[35,37,46], [2,1,3], [5,3,8] ...} Any help with this would be greatly appreciated. And thank you so much for all of your help thus far, I'm still pretty new to python and am enjoying all of the flexibility associated with a scripting and programming language. Thanks again, Krishna Simon Forman sajmik...@gmail.com 08/11/09 12:15 PM On Aug 11, 11:51 am, MRAB pyt...@mrabarnett.plus.com wrote: Krishna Pacifici wrote: Thanks for the help. Actually this is part of a much larger project, but I have unfortunately pigeon-holed myself into needing to do these things without a whole lot of flexibility. To give a specific example I have the following dictionary where I need to remove values that are duplicated with other values and remove values that are duplicates of the keys, but still retain it as a dictionary. Each value is itself a class with many attributes that I need to call later on in the program, but I cannot have duplicates because it would mess up some estimation part of my model. d = {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]} So I want a new dictionary that would get rid of the duplicate values of 21, 22, 36 and 20 and give me back a dictionary that looked like this: new_d= {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12, 32], 26: [25, 27, 16], 30: [40]} I understand that a dictionary may not be the best approach, but like I said I have sort of pigeon-holed myself by the way that I am simulating my data and the estimation model that I am using. Any suggestions or comments about the above problem would be greatly appreciated. d = {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]} new_d = {} seen = set(d.keys()) for k, v in d.items(): ... new_d[k] = [x for x in v if x not in seen] ... seen |= set(new_d[k]) ... new_d {36: [35, 37, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12, 32], 26: [25, 27, 16], 30: [40]} Ha ha, MRAB beat me to it: d = { 36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40], } new_d = { # Given, and apparently incorrect. 36: [35, 37, 26, 46], # 26 is a key and should be gone. 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12, 32], 26: [25, 27, 16], 30: [40], } expected = { 36: [35, 37, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12, 32], 26: [25, 27, 16], 30: [40], } def removeDuplicates(D): ''' Remove values that are duplicated with other values and remove values that are duplicates of the keys. Assumes that values in the lists are already unique within each list. I.e. duplicates are only in the keys or in other lists. This function works in place on D, so it doesn't return anything. Caller must keep a reference to D. ''' seen = set(D) # Get a set of the keys. for key, values_list in D.iteritems(): # Filter out values that have already been seen. filtered_values = [ value for value in values_list if not value in seen ] # Remember newly seen values. seen.update(filtered_values) D[key] = filtered_values ## Example: ## ## d == expected ##False ## removeDuplicates(d) ## d == expected ##True -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: looping through values in a dictionary and appending to a list
Nevermind, got it. Sorry. Krishna Pacifici 08/11/09 2:12 PM Hi, I want to append the values of a dictionary to a list. I have a dictionary sec_dict_clean and I want to append the values to a list, but am having a hard time looping through the values in the dictionary. I have tried something like this: lista=[] for i in sec_dict_clean.values(): for j in sec_dict_clean.values()[i]: lista.append(sec_dict_clean.values()[i]) But I keep on getting an error: TypeError: list indices must be integers Any ideas on how to loop through values in a dictionary? Thanks, Krishna pacifi...@warnell.uga.edu/pacifi...@warnell.uga.edu -- http://mail.python.org/mailman/listinfo/python-list
adding multiple new values to the same key in a dictionary
Hi, I want to be able to add multiple new values to a key in a dictionary. I have tried the following: sec_dict_clean= {88: [87, 89, 78, 98], 58: [57, 59, 48, 68], 69: [79], 95: [94, 96, 85]} for i in range(len(sec_dict_clean.values())): for j in range(len(sec_dict_clean.values()[i])): sec_dict_clean.setdefault(key,[]).append(blocks[sec_dict_clean.values()[i][j]].abundance) where blocks[...].abundance gives me a single value from an object, but this gives me the following: sec_dict_clean= {88: [87, 89, 78, 98], 58: [57, 59, 48, 68], 69: [79], 95: [94, 96, 85, 4, 12, 11, 6, 9, 12, 11, 7, 10, 10, 12, 9, 6, 12, 15, 9, 8, 12, 15, 12, 12]} instead I want each abundance (starts with 4, 12...) to be associated with each of the values so that it would look like this: sec_dict_clean= {88: ([87, 89, 78, 98], [4,12,11,6]), 58: ([57, 59, 48, 68], [9,12,11,7]), 69: ([79], [10])...} You can see there are several errors here (I have more things being appended than there are values in the dictionary), but I really just want to know how to add multiple values to the same key in a dictionary. Thanks for any help, Krishna -- http://mail.python.org/mailman/listinfo/python-list