Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 14th, 2005, 06:05 PM
rudysanford@hotmail.com
Guest
 
Posts: n/a
Default Worthwhile to reverse a dictionary

I just started messing with programming and started with Python. Part
of my first project deals with translating numerical values to letters.
I would like to be able to do the reverse as well, letters to numbers,
sort of a table for lookup and a table for reverse lookup. I thought
that a dictionary would be the structure to use- write it once and in a
second instance swap the keys for values and values for keys. However,
reversing a dictionary does not seem to be easily achieved. Am I using
the wrong data structure?

  #2  
Old December 14th, 2005, 06:15 PM
Will McGugan
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary

rudysanford@hotmail.com wrote:[color=blue]
> I just started messing with programming and started with Python. Part
> of my first project deals with translating numerical values to letters.
> I would like to be able to do the reverse as well, letters to numbers,
> sort of a table for lookup and a table for reverse lookup. I thought
> that a dictionary would be the structure to use- write it once and in a
> second instance swap the keys for values and values for keys. However,
> reversing a dictionary does not seem to be easily achieved. Am I using
> the wrong data structure?
>[/color]

There may be a better solution to your original problem (if you post
more details Im sure there will be plenty of suggestions), but the
following should reverse a dictionary..
[color=blue][color=green][color=darkred]
>>> testdict = dict(a=1, b=2)
>>> reversedict = dict( (value, key) for key, value in[/color][/color][/color]
testdict.iteritems() )

Will McGugan
--
http://www.willmcgugan.com
"".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")
  #3  
Old December 14th, 2005, 06:55 PM
rudysanford@hotmail.com
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary

Thanks so much. That seems to have it. This is the sort of thing I
had before:


#!/usr/local/bin/python



# make a short dictionary
d1 = {'A' : '1', 'B' : '2', 'C' : '3'}

for letter in d1.keys(): print letter, '\t', d1[letter]

# make a space between the output of the 2 dictionaries
print '\n'


# reverse the keys and values from the first dictionary
# from this point many things were tried, about the best I achieved was
# getting the last key/value pair as displayed when printing the d1
# it was properly reversed but there was only one pair
d2 = {d1[letter] : letter}

for num in d2.keys(): print num, '\t', d2[num]


This displays
A 1
C 3
B 2

2 B

  #4  
Old December 14th, 2005, 07:05 PM
rudysanford@hotmail.com
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary

What is the difference between

" d1 = {'A' : '1', 'B' : '2', 'C' : '3'} "

and

" d1 = dict(A = 1, B = 2, C = 3) " ?

All of the dictionary examples I saw (python.org, aspn.activestate.com,
Learning Python by Lutz, among others) use d={'x' : 'y'}.

  #5  
Old December 14th, 2005, 07:35 PM
Erik Max Francis
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary

rudysanford@hotmail.com wrote:
[color=blue]
> What is the difference between
>
> " d1 = {'A' : '1', 'B' : '2', 'C' : '3'} "
>
> and
>
> " d1 = dict(A = 1, B = 2, C = 3) " ?
>
> All of the dictionary examples I saw (python.org, aspn.activestate.com,
> Learning Python by Lutz, among others) use d={'x' : 'y'}.[/color]

In the latter case the values are ints, whereas in the former they are
strings. But you probably didn't mean that; indeed it is the case that

d1 = {'A': 1, 'B': 2, 'C': 3}

and

d2 = dict(A=1, B=2, C=3)

are equivalent.

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
I do not promise to consider race or religion in my appointments. I
promise only that I will not consider them. -- John F. Kennedy
  #6  
Old December 14th, 2005, 08:15 PM
rudysanford@hotmail.com
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary

Thanks. That is exactly what I meant.

In d2, A is not 'A' because it is being set to 1, whereas in d1, using
A instead of 'A' means A is an undeclared variable instead of a string?

  #7  
Old December 15th, 2005, 02:45 AM
Mike Meyer
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary

Erik Max Francis <max@alcyone.com> writes:[color=blue]
> rudysanford@hotmail.com wrote:[color=green]
>> What is the difference between
>> " d1 = {'A' : '1', 'B' : '2', 'C' : '3'} "
>> and
>> " d1 = dict(A = 1, B = 2, C = 3) " ?
>> All of the dictionary examples I saw (python.org,
>> aspn.activestate.com,
>> Learning Python by Lutz, among others) use d={'x' : 'y'}.[/color]
>
> In the latter case the values are ints, whereas in the former they are
> strings. But you probably didn't mean that; indeed it is the case that
> d1 = {'A': 1, 'B': 2, 'C': 3}
> and
> d2 = dict(A=1, B=2, C=3)
> are equivalent.[/color]

Not quite:
[color=blue][color=green][color=darkred]
>>> def f():[/color][/color][/color]
.... a = {'a': 1, 'b': 2}
.... b = dict(a = 1, b = 2)
....[color=blue][color=green][color=darkred]
>>> dis.dis(f)[/color][/color][/color]
2 0 BUILD_MAP 0
3 DUP_TOP
4 LOAD_CONST 1 ('a')
7 LOAD_CONST 2 (1)
10 ROT_THREE
11 STORE_SUBSCR
12 DUP_TOP
13 LOAD_CONST 3 ('b')
16 LOAD_CONST 4 (2)
19 ROT_THREE
20 STORE_SUBSCR
21 STORE_FAST 0 (a)

3 24 LOAD_GLOBAL 1 (dict)
27 LOAD_CONST 1 ('a')
30 LOAD_CONST 2 (1)
33 LOAD_CONST 3 ('b')
36 LOAD_CONST 4 (2)
39 CALL_FUNCTION 512
42 STORE_FAST 1 (b)
45 LOAD_CONST 0 (None)
48 RETURN_VALUE


The first form builds the dict in place, and assigns the result. The
second form invokes the "dict" object on the keyword arguments, which
function then builds the dict. They have the same effect, provided no
one has shadowed the definition of the builtin "dict" function.

<mike
--
Mike Meyer <mwm@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
  #8  
Old December 15th, 2005, 06:05 PM
Kent Johnson
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary

rudysanford@hotmail.com wrote:[color=blue]
> What is the difference between
>
> " d1 = {'A' : '1', 'B' : '2', 'C' : '3'} "
>
> and
>
> " d1 = dict(A = 1, B = 2, C = 3) " ?
>
> All of the dictionary examples I saw (python.org, aspn.activestate.com,
> Learning Python by Lutz, among others) use d={'x' : 'y'}.
>[/color]

The second form is only available in Python 2.3 and newer, so any example that is older
than that will use the first form.

Kent
  #9  
Old December 15th, 2005, 06:35 PM
Larry Bates
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary

rudysanford@hotmail.com wrote:[color=blue]
> I just started messing with programming and started with Python. Part
> of my first project deals with translating numerical values to letters.
> I would like to be able to do the reverse as well, letters to numbers,
> sort of a table for lookup and a table for reverse lookup. I thought
> that a dictionary would be the structure to use- write it once and in a
> second instance swap the keys for values and values for keys. However,
> reversing a dictionary does not seem to be easily achieved. Am I using
> the wrong data structure?
>[/color]
This doesn't really answer your question (others have already done this),
but back in the 'ol days we did this:
[color=blue][color=green][color=darkred]
>>> letters=['a','b','c']
>>> map(lambda x: ord(x)-64, letters)[/color][/color][/color]
[1, 2, 3]

This takes advantage of the fact that letters must be stored as
binary integers in ASCII (e.g. 'a' = 65, 'b'=66, etc.). You can
go the other direction with chr(x). Not completely sure about
what you want to accomplish, but this eliminates the need for
the dictionaries.

Larry Bates
  #10  
Old December 15th, 2005, 09:15 PM
rudysanford@hotmail.com
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary

The end of what I was trying to do was encode and decode using ITA2
International Telegraph Alphabet 2, more commonly called Baudot. It
uses 5 bit binary but with the use of a shift up or a shift down can
utilize 2 tables of 32- one for letters one for figures.

A better explanation of ITA2 here: http://en.wikipedia.org/wiki/Baudot

It occurred to me that rather than writing for four data dictionaries
(bin to ltrs, bin to figs, figs, to bin, figs to letters), I could just
swap the dictionary key/value pairs around.

It seems that I have more reading to do. I have not gotten to lambda
formations yet.

  #11  
Old January 4th, 2006, 10:15 PM
crc
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary


I assume your talking about building a new dictionary with the key and
value pair switched. I have seen no built in function to do this but I
have found a way to create another dictionary that is the inverse of
the first.

for example you have designed a dictionary such as

a={}
a['one']=1
a['two']=2
a['three']=3
a['four']=4

when your redy to build a revers dictionary you semply do the folowing
b={}
for x in a:
temp_idx=a[x]
b[temp_idx]=x

now you get
{'four': 4, 'three': 3, 'two': 2, 'one': 1}
for a

and

{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
for b


--
crc
Message posted via http://www.exforsys.com for all your training needs.

  #12  
Old January 4th, 2006, 11:05 PM
Mike Meyer
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary

crc <crc.214rin@no-mx.forums.yourdomain.com.au> writes:[color=blue]
> I assume your talking about building a new dictionary with the key and
> value pair switched. I have seen no built in function to do this but I
> have found a way to create another dictionary that is the inverse of
> the first.[/color]

Note that you can't reliable "reverse" a dictionary. That's because
you can put anything in a dictionary, but some things can't be used as
keys to a dictionary.
[color=blue]
> when your redy to build a revers dictionary you semply do the folowing
> b={}
> for x in a:
> temp_idx=a[x]
> b[temp_idx]=x[/color]

You can do it with a single list comprehension:

b = dict([(x, y) for y, x in a.iteritems()])

<mike
--
Mike Meyer <mwm@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
  #13  
Old January 8th, 2006, 06:55 AM
Alex Martelli
Guest
 
Posts: n/a
Default Re: Worthwhile to reverse a dictionary

Mike Meyer <mwm@mired.org> wrote:
[color=blue]
> crc <crc.214rin@no-mx.forums.yourdomain.com.au> writes:[color=green]
> > I assume your talking about building a new dictionary with the key and
> > value pair switched. I have seen no built in function to do this but I
> > have found a way to create another dictionary that is the inverse of
> > the first.[/color]
>
> Note that you can't reliable "reverse" a dictionary. That's because
> you can put anything in a dictionary, but some things can't be used as
> keys to a dictionary.[/color]

That's reason #1. Reason #2 is that you can have duplicates among a
dict's values, but not among its keys. So, a simply and correctly
reversable dict is one whose values are all hashable and unique -- in
terms of EQUALITY (which is stricter than demanding uniqueness in terms
of IDENTITY), e.g. you can't have both 1 (the integer) and 1.0 (the
float with a 0 fractional part) as values (because they're not distinct
by equality: 1 == 1.0).

If you do have a dict whose hashable values are not guaranteed to be
unique you have several choices as to how to "pseudo-reverse" it (e.g.,
"dropping", among the values in the pseudoreversed dict, all but one of
the keys that map to the same value in the original dict; or, making a
"reversoid" dict whose values are containers of all the keys mapping to
each value in the original dict).

If you have a dict whose values are not guaranteed to be hashable, and
thus can't be keys in the ``sotospeakreverse'' dict, you have to work
even harder, striving to build keys for the latter which may in some
sense (probably one meaningful only to a specific application) "stand
for" the values in the original dict. I remember only one case in a
real-world program of needing anything like that -- the original dict
had "sub"-dicts (mapping strings to numbers) as values, but fortunately
it was acceptable for the application to have frozen sets of (key,
value) pairs "stand for" those subsets in the "reversoid" dict (I sure
did wish at the time I had "frozen dicts", though;-).


Alex
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles