<html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1250"><META name="Author" content="Novell GroupWise WebAccess"></head><body style='font-family: Tahoma, sans-serif; font-size: 13px; '>Wow, thanks MRAB and Simon, you guys are good.<br><br>I guess I will go ahead and ask the next question that has also stumped me for awhile now.<br><br>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.<br><br>So again it would look something like this:<br><br>block 35 has 2 attributes, say a and b, a=2, b=5 <br>block 37 has a=1, b=3<br>block 46 has a=3, b=8<br><br>the two attributes come from two different definitions within the class statement, <br>def detections<br>...<br>return a<br><br>def abundance<br>...<br>return b<br><br>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:<br>                         a           b   <br>{36:[35,37,46], [2,1,3], [5,3,8] ...}<br><br>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.<br><br>Thanks again,<br>Krishna<br><br>>>> Simon Forman <sajmikins@gmail.com> 08/11/09 12:15 PM >>><br>On Aug 11, 11:51 am, MRAB <pyt...@mrabarnett.plus.com> wrote:<br>> Krishna Pacifici wrote:<br>> > Thanks for the help.<br>><br>> > Actually this is part of a much larger project, but I have unfortunately<br>> > pigeon-holed myself into needing to do these things without a whole lot<br>> > of flexibility.<br>><br>> > To give a specific example I have the following dictionary where I need<br>> > to remove values that are duplicated with other values and remove values<br>> > that are duplicates of the keys, but still retain it as a dictionary.  <br>> > Each value is itself a class with many attributes that I need to call<br>> > later on in the program, but I cannot have duplicates because it would<br>> > mess up some estimation part of my model.<br>><br>> > d =<br>> > {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22:<br>> > [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}<br>><br>> > So I want a new dictionary that would get rid of the duplicate values of<br>> > 21, 22, 36 and 20 and give me back a dictionary that looked like this:<br>><br>> > new_d=<br>> > {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23,<br>> > 12, 32], 26: [25, 27, 16], 30: [40]}<br>><br>> > I understand that a dictionary may not be the best approach, but like I<br>> > said I have sort of pigeon-holed myself by the way that I am simulating<br>> > my data and the estimation model that I am using.  Any suggestions or<br>> > comments about the above problem would be greatly appreciated.<br>><br>>  >>> d = {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11,<br>> 31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}<br>>  >>> new_d = {}<br>>  >>> seen = set(d.keys())<br>>  >>> for k, v in d.items():<br>> ...     new_d[k] = [x for x in v if x not in seen]<br>> ...     seen |= set(new_d[k])<br>> ...<br>>  >>> new_d<br>> {36: [35, 37, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12,<br>> 32], 26: [25, 27, 16], 30: [40]}<br><br>Ha ha, MRAB beat me to it:<br><br>d = {<br>    36: [35, 37, 26, 46],<br>    75: [74, 76, 65, 85],<br>    21: [20, 22, 11, 31],<br>    22: [21, 23, 12, 32],<br>    26: [25, 27, 16, 36],<br>    30: [20, 31, 40],<br>    }<br><br><br>new_d = { # Given, and apparently incorrect.<br>    36: [35, 37, 26, 46], # 26 is a key and should be gone.<br>    75: [74, 76, 65, 85],<br>    21: [20, 11, 31],<br>    22: [23, 12, 32],<br>    26: [25, 27, 16],<br>    30: [40],<br>    }<br><br><br>expected = {<br>    36: [35, 37, 46],<br>    75: [74, 76, 65, 85],<br>    21: [20, 11, 31],<br>    22: [23, 12, 32],<br>    26: [25, 27, 16],<br>    30: [40],<br>    }<br><br><br>def removeDuplicates(D):<br>    '''<br>    Remove values that are duplicated with other values<br>    and remove values that are duplicates of the keys.<br><br>    Assumes that values in the lists are already unique within<br>    each list.  I.e. duplicates are only in the keys or in other<br>    lists.<br><br>    This function works "in place" on D, so it doesn't return<br>    anything.  Caller must keep a reference to D.<br>    '''<br><br>    seen = set(D) # Get a set of the keys.<br><br>    for key, values_list in D.iteritems():<br><br>        # Filter out values that have already been seen.<br>        filtered_values = [<br>            value<br>            for value in values_list<br>            if not value in seen<br>            ]<br><br>        # Remember newly seen values.<br>        seen.update(filtered_values)<br><br>        D[key] = filtered_values<br><br><br>## Example:<br>##<br>##    >>> d == expected<br>##    False<br>##    >>> removeDuplicates(d)<br>##    >>> d == expected<br>##    True<br>-- <br>http://mail.python.org/mailman/listinfo/python-list<br></body></html>