tag:blogger.com,1999:blog-35609023.post4533934079118896517..comments2022-04-08T20:26:13.393+02:00Comments on nominolo's Blog: A short reminderThomas Schillinghttp://www.blogger.com/profile/04274984206279511399noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-35609023.post-51511026861620381342008-04-02T17:38:00.000+02:002008-04-02T17:38:00.000+02:00# in python:def invertSetMap(sm):  ...# in python:<BR/>def invertSetMap(sm):<BR/> r = {}<BR/> for k, vs in a.iteritems():<BR/> for v in vs:<BR/> r.setdefault(v, set()).add(k)<BR/> return rAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-35609023.post-73748417667316934862008-04-02T16:48:00.000+02:002008-04-02T16:48:00.000+02:00in python:def invertSetMap(sm): r = {} for k...in python:<BR/><BR/>def invertSetMap(sm):<BR/> r = {}<BR/> for k, vs in a.iteritems():<BR/> for v in vs:<BR/> r.setdefault(v, set()).add(k)<BR/> return rAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-35609023.post-59775238692177956542008-03-18T12:08:00.000+01:002008-03-18T12:08:00.000+01:00If you represent sets as maps with unit values, I ...If you represent sets as maps with unit values, I think the most straightforward Perl solution is:<BR/>my %result;<BR/>for my $outer (keys %x) {<BR/> for my $inner (keys %{$x{$outer}}) {<BR/> $result{$inner}{$outer} = undef;<BR/> }<BR/>}<BR/><BR/>Not very elegant, but it gets the job done.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-35609023.post-31851806663097268172008-03-17T13:07:00.000+01:002008-03-17T13:07:00.000+01:00Yes, Python has list comprehensions, too, and I gu...Yes, Python has list comprehensions, too, and I guess they're also efficient. Note, though that my code requires nested "loops", since it stores sets of values rather than just a single value. I don't know what Python programmers use as sets, but if you decide to use lists, the outputs should be:<BR/>{1: ['a'], 2: ['b']}<BR/>and<BR/>{1: ['a','b']}Thomas Schillinghttps://www.blogger.com/profile/04274984206279511399noreply@blogger.comtag:blogger.com,1999:blog-35609023.post-47124399491314743972008-03-17T13:01:00.000+01:002008-03-17T13:01:00.000+01:00And in Perl:> perl -MData::Dumper -e '%x=(a=>1,b=>...And in Perl:<BR/><BR/>> perl -MData::Dumper -e '%x=(a=>1,b=>1);%y=reverse %x;print Dumper \%y'Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-35609023.post-59996533959140196112008-03-17T05:51:00.000+01:002008-03-17T05:51:00.000+01:00Here's a Python version, just for kicks:>>> def in...Here's a Python version, just for kicks:<BR/><BR/>>>> def invert_dict( input_dictionary ):<BR/> return dict( [ ( value, key ) for ( key, value ) in input_dictionary.iteritems() ] )<BR/>...<BR/>>>> invert_dict( { 'a':1, 'b':2 } )<BR/>{1: 'a', 2: 'b'}<BR/>>>> invert_dict( { 'a':1, 'b':1 } )<BR/>{1: 'b'}Shashankhttps://www.blogger.com/profile/02065604028497476702noreply@blogger.com