4
alphaValueDict = OrderedDict.fromkeys(string.ascii_uppercase,range(0)
i = 1
for k,v in alphaValueDict.iteritems():
    alphaValueDict[k] = [i]
    i += 1
return alphaValueDict

我需要创建一个有序的 dict ,其中键是字母表中的所有字母,值是 1 - 26。我的问题是,如何使用 dict 理解在一行中执行此操作?

4

9 回答 9

10

您可以完全避免 dict-comprehension:

>>> import string
>>> dict(zip(string.ascii_lowercase, range(1,27)))
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's': 19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}

有一个OrderedDict

>>> import string
>>> from collections import OrderedDict
>>> OrderedDict(zip(string.ascii_lowercase, range(1,27)))
OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7), ('h', 8), ('i', 9), ('j', 10), ('k', 11), ('l', 12), ('m', 13), ('n', 14), ('o', 15), ('p', 16), ('q', 17), ('r', 18), ('s', 19), ('t', 20), ('u', 21), ('v', 22), ('w', 23), ('x', 24), ('y', 25), ('z', 26)])

仅当您必须进行更多计算以获取键/值或增强可读性时,我才会使用 dict-comprehension(极端示例:{noun : age for noun, age in something()}让您了解我们在说什么,而dict(something())没有)。

于 2013-10-23T13:27:55.957 回答
4

我的看法:

from string import ascii_uppercase
from collections import OrderedDict

od = OrderedDict((ch, idx) for idx, ch in enumerate(ascii_uppercase, 1))

或者:

from itertools import count, izip
od = OrderedDict(izip(ascii_uppercase, count(1)))
于 2013-10-23T13:27:29.660 回答
3

用于enumerate()计算字母:

>>> OrderedDict((k,v+1) for v,k in enumerate(string.ascii_uppercase))
OrderedDict([('A', 1), ('B', 2), ('C', 3), ('D', 4), ('E', 5), ('F', 6), ('G', 7), ('H', 8), ('I', 9), ('J', 10), ('K', 11), ('L', 12), ('M', 13), ('N', 14), ('O', 15), ('P', 16), ('Q', 17), ('R', 18), ('S', 19), ('T', 20), ('U', 21), ('V', 22), ('W', 23), ('X', 24), ('Y', 25), ('Z', 26)])

要回答帖子末尾的问题,dict 理解不是初始化OrderedDict. 字典理解只创建普通字典,如果你用它来初始化你OrderedDict的已经太晚了,你已经失去了任何排序。

于 2013-10-23T13:27:15.627 回答
3
theDict ={chr(y):y-64 for y in range(65,91)
print theDict

输出:

{'A':1,'B':2,......'X':24,'Y':25,'Z':26}
于 2018-01-09T04:05:39.337 回答
2

最简单的方法是使用 zip 和 tuple 解包

import string
{ k: v for k, v in zip(string.ascii_uppercase, xrange(1, len(string.ascii_uppercase)+1))}

编辑以考虑评论。这是一个单行,没有 dict comp。只是依靠 OrderedDict 采用可迭代和 zip 的力量。

OrderedDict(zip(string.ascii_uppercase, xrange(1, len(string.ascii_uppercase)+1)))
于 2013-10-23T13:24:10.490 回答
2

使用基本:

>>> from string import ascii_uppercase
>>> from collections import OrderedDict

一个班轮:

>>> OrderedDict((k, i+1) for i, k in enumerate(ascii_uppercase))

产量:

OrderedDict([('A', 1), ('B', 2), ('C', 3), ('D', 4), ('E', 5), ('F', 6), ('G', 7), ('H', 8), ('I', 9), ('J', 10), ('K', 11), ('L', 12), ('M', 13), ('N', 14), ('O', 15), ('P', 16), ('Q', 17), ('R', 18), ('S', 19), ('T', 20), ('U', 21), ('V', 22), ('W', 23), ('X', 24), ('Y', 25), ('Z', 26)])
于 2013-10-23T13:30:31.397 回答
1
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> s= string.ascii_lowercase

>>> from collections import OrderedDict
>>> odict = OrderedDict()
>>> for count,i in enumerate(s):
...     odict[count] = i
... 
>>> odict
OrderedDict([(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g'), (7, 'h'), (8, 'i'), (9, 'j'), (10, 'k'), (11, 'l'), (12, 'm'), (13, 'n'), (14, 'o'), (15, 'p'), (16, 'q'), (17, 'r'), (18, 's'), (19, 't'), (20, 'u'), (21, 'v'), (22, 'w'), (23, 'x'), (24, 'y'), (25, 'z')])
于 2013-10-23T13:22:14.510 回答
0
print {chr(ele+97): ele+1 for ele in xrange(26)}
于 2013-10-23T13:25:44.403 回答
0

不使用字典理解,变体扩展dict

import string

class alph(dict):
    def __getitem__(self, key):
        if len(key) != 1:
            raise ValueError("Not a character.")
        return string.ascii_lowercase.index(key.lower()) + 1

处理上下键并抛出ValueError错误的键。

于 2013-10-23T13:39:24.647 回答