值得翻转字典 [英] Worthwhile to reverse a dictionary
问题描述
我刚开始搞乱编程并开始使用Python。部分
我的第一个项目涉及将数值转换为字母。
我希望能够反向执行,字母到数字,
一种用于查找的表和一种用于反向查找的表。我认为
字典是要使用的结构 - 写一次并在第二个实例中交换键的值和值的键。但是,翻译一本字典的
似乎并不容易实现。我使用
错误的数据结构吗?
ru ********* @ hotmail.com 写道:我刚刚开始搞乱编程并开始用Python。我的第一个项目部分涉及将数值转换为字母。
我希望能够反向执行,字母转换为数字,
排序查找表格和用于反向查找的表。我想
字典将是使用的结构 - 只需编写一次,然后在第二个实例中交换密钥的值和值的键。但是,翻译字典似乎并不容易实现。我是否使用了错误的数据结构?
原始问题可能有更好的解决方案(如果你发布了
更多细节我肯定会有很多建议),但
以下应该反转字典..
< blockquote class =post_quotes> testdict = dict(a = 1,b = 2)
reversedict = dict(键,键值),中的值
< br $> b $ b testdict.iteritems())
麦克古根将会是什么?b $ b -
http://www.willmcgugan.com
"" .join({''*' ':''''',''^'':''。''}。得到(c,0)或chr(97+(ord(c)-84)%26)for
" jvyy * jvyyzpthtna ^ pbz")
非常感谢。这似乎有它。这是我以前用过
的那种东西:
#!/ usr / local / bin / python
#make一个简短的字典
d1 = {''A'':''1'',''B'':''2'',''C'':''3''}
d1.keys()中的字母:打印字母,''\ t'',d1 [letter]
#make 2个字典的输出之间的空格
print''\ n''
#反转第一个字典中的键和值
#从这一点开始尝试很多东西,关于我取得的最好成绩是
#获取打印d1时显示的最后一个键/值对
#这是正确的反转,但只有一对
d2 = {d1 [letter]:letter}
为d2.keys()中的num:print num,' '\t'',d2 [num]
显示
A 1
C 3
B 2
2 B
I just started messing with programming and started with Python. Part
" d1 = {''A'':''1'',''B'':''2'',''C'':''3''}"
和
" d1 = dict(A = 1,B = 2,C = 3) ?
我看到的所有字典示例(python.org,aspn.activestate.com,
由Lutz学习Python等)使用d = {''x'':''y''}。
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?
ru*********@hotmail.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.iteritems() )
Will McGugan
--
http://www.willmcgugan.com
"".join({''*'':''@'',''^'':''.''}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^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.activestate.com,
Learning Python by Lutz, among others) use d={''x'' : ''y''}.
这篇关于值得翻转字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!