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? 12 3160 ru*********@hot mail.com wrote: 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?
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.. testdict = dict(a=1, b=2) reversedict = dict( (value, key) for key, value in
testdict.iterit ems() )
Will McGugan
-- http://www.willmcgugan.com
"".join({'*':'@ ','^':'.'}.get( c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzptht na^pbz")
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
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.activestat e.com,
Learning Python by Lutz, among others) use d={'x' : 'y'}. ru*********@hot mail.com wrote: 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.activestat e.com, Learning Python by Lutz, among others) use d={'x' : 'y'}.
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 && ma*@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
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?
Erik Max Francis <ma*@alcyone.co m> writes: ru*********@hot mail.com wrote: 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.activestat e.com, Learning Python by Lutz, among others) use d={'x' : 'y'}.
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.
Not quite: def f():
.... a = {'a': 1, 'b': 2}
.... b = dict(a = 1, b = 2)
.... dis.dis(f)
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 <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. ru*********@hot mail.com wrote: 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.activestat e.com, Learning Python by Lutz, among others) use d={'x' : 'y'}.
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 ru*********@hot mail.com wrote: 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?
This doesn't really answer your question (others have already done this),
but back in the 'ol days we did this: letters=['a','b','c'] map(lambda x: ord(x)-64, letters)
[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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: tjland |
last post by:
Okay so im working on a very simple encryption method using just loops.
Kind of novel i think. Okay so first i set up a list of the alphabet with
just every seperate letter, then user is prompted for a word, this is not
user friendly just for me. Ok now as you can see below this is pretty
basic, if u can follow all the loops i get to a point where I have the
letter positions in the list for the final word but i dont know of a way
to come...
|
by: Raymond Hettinger |
last post by:
Here is a discussion draft of a potential PEP.
The ideas grew out of the discussion on pep-284.
Comments are invited. Dart throwing is optional.
Raymond Hettinger
-------------------------------------------------------------
PEP: 323
|
by: Raymond Hettinger |
last post by:
Based on the feedback here on comp.lang.python, the pep has been
updated:
www.python.org/peps/pep-0322.html
The key changes are:
* reversed() is being preferred to ireverse() as the best name.
|
by: Brad Tilley |
last post by:
A friend of mine wrote an algorithm that generates strings. He says that
it's impossible to figure out exactly how the algorithm works. That may
be true, but I think if I had enough sample strings that I could write a
program to identify patterns in the strings and then produce strings
with similar patterns. He disagrees with me. He gave me 100 strings to
analyze.
I wrote a script to read each string and build a list of characters...
|
by: tkpmep |
last post by:
I have list of lists of the following form
L=, , , ]
I want to aggregate these lists, i.e. to reduce L to
L=, ] #500 = 100+400, 200=300-100
Here's how I have done it:
L.sort()
for i in range(len(L),0,-1):
| |
by: xiao zhang yu |
last post by:
me was sorry if this question are present before
DotNet, no matter VB.Net or C# all they are compiled to IL,
and yes, that IL will totally same as "open-sourse",
every IL will easy to decompile and get the source nicety,
although there have comeout with some "obfuscators" solution,
but the structure still remain exactly same as the source after obfuscate.
me unable to understand what the benefit will IL bring to me,
is that benefit of...
|
by: rh0dium |
last post by:
Hi all,
I have a dict which looks like this..
dict={'130nm': {'umc': },
'180nm': {'chartered': , 'tsmc':
},
'250nm': {'umc': , 'tsmc':
}
}
|
by: rick |
last post by:
Why can't Python have a reverse() function/method like Ruby?
Python:
x = 'a_string'
# Reverse the string
print x
Ruby:
x = 'a_string'
# Reverse the string
|
by: KG |
last post by:
Could any one tell me how to reverse the bits in an interger?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |