使用Python 3.0,我将如何创建100个索引的列表,如下所示:
grid_Sys = [['A0'],['A1'],['A2']....['A9'],['B0'],...[J9]]
我理解如何通过使用ord()和chr()函数来增加字母的顺序。但是,在切换chr(ord('A')+1) = B
之前,我不明白如何进行10索引。
基本上,我想达到一个可以做到这样的事情:
grid_Sys = [['A0','brown']...
但是,这只是一个随机颜色的简单附加选项。
答案 0 :(得分:7)
你不需要那么复杂。只需使用:
[['%s%s'%(i,j)] for i in 'ABCDEFGHIJ' for j in range(10)]
没有ord(..)
,chr(..)
或复杂的公式:简单明了一个可读的陈述,显示你想要构建的东西:通常如果你不得不做不可读的事情,你做错了。
这给出了:
>>> [['%s%s'%(i,j)] for i in 'ABCDEFGHIJ' for j in range(10)]
[['A0'], ['A1'], ['A2'], ['A3'], ['A4'], ['A5'], ['A6'], ['A7'], ['A8'], ['A9'], ['B0'], ['B1'], ['B2'], ['B3'], ['B4'], ['B5'], ['B6'], ['B7'], ['B8'], ['B9'], ['C0'], ['C1'], ['C2'], ['C3'], ['C4'], ['C5'], ['C6'], ['C7'], ['C8'], ['C9'], ['D0'], ['D1'], ['D2'], ['D3'], ['D4'], ['D5'], ['D6'], ['D7'], ['D8'], ['D9'], ['E0'], ['E1'], ['E2'], ['E3'], ['E4'], ['E5'], ['E6'], ['E7'], ['E8'], ['E9'], ['F0'], ['F1'], ['F2'], ['F3'], ['F4'], ['F5'], ['F6'], ['F7'], ['F8'], ['F9'], ['G0'], ['G1'], ['G2'], ['G3'], ['G4'], ['G5'], ['G6'], ['G7'], ['G8'], ['G9'], ['H0'], ['H1'], ['H2'], ['H3'], ['H4'], ['H5'], ['H6'], ['H7'], ['H8'], ['H9'], ['I0'], ['I1'], ['I2'], ['I3'], ['I4'], ['I5'], ['I6'], ['I7'], ['I8'], ['I9'], ['J0'], ['J1'], ['J2'], ['J3'], ['J4'], ['J5'], ['J6'], ['J7'], ['J8'], ['J9']]
然而,我不明白你为什么要构建列表列表?通常,如果您想在坐标或其他值上“附加”属性,则使用 dict
ionary 。
答案 1 :(得分:4)
我可能会使用string.ascii_uppercase
和itertools
的组合:
>>> import itertools
>>> import string
>>> p = itertools.product(string.ascii_uppercase, range(10))
>>> list(itertools.islice(p, 100)) # Take the first 100
[('A', 0), ('A', 1), ('A', 2), ('A', 3), ('A', 4), ('A', 5), ('A', 6), ('A', 7), ('A', 8), ('A', 9), ('B', 0), ('B', 1), ('B', 2), ('B', 3), ('B', 4), ('B', 5), ('B', 6), ('B', 7), ('B', 8), ('B', 9), ('C', 0), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5), ('C', 6), ('C', 7), ('C', 8), ('C', 9), ('D', 0), ('D', 1), ('D', 2), ('D', 3), ('D', 4), ('D', 5), ('D', 6), ('D', 7), ('D', 8), ('D', 9), ('E', 0), ('E', 1), ('E', 2), ('E', 3), ('E', 4), ('E', 5), ('E', 6), ('E', 7), ('E', 8), ('E', 9), ('F', 0), ('F', 1), ('F', 2), ('F', 3), ('F', 4), ('F', 5), ('F', 6), ('F', 7), ('F', 8), ('F', 9), ('G', 0), ('G', 1), ('G', 2), ('G', 3), ('G', 4), ('G', 5), ('G', 6), ('G', 7), ('G', 8), ('G', 9), ('H', 0), ('H', 1), ('H', 2), ('H', 3), ('H', 4), ('H', 5), ('H', 6), ('H', 7), ('H', 8), ('H', 9), ('I', 0), ('I', 1), ('I', 2), ('I', 3), ('I', 4), ('I', 5), ('I', 6), ('I', 7), ('I', 8), ('I', 9), ('J', 0), ('J', 1), ('J', 2), ('J', 3), ('J', 4), ('J', 5), ('J', 6), ('J', 7), ('J', 8), ('J', 9)]
如果你想把它格式化为字符串,那也很容易:
>>> p = itertools.product(string.ascii_uppercase, range(10))
>>> ['%s%d'%t for t in itertools.islice(p, 100)]
['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'E0', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8', 'E9', 'F0', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'G0', 'G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'H0', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'I0', 'I1', 'I2', 'I3', 'I4', 'I5', 'I6', 'I7', 'I8', 'I9', 'J0', 'J1', 'J2', 'J3', 'J4', 'J5', 'J6', 'J7', 'J8', 'J9']
(将它们放在子列表中也应该很容易...... [['%s%d' % t] for t in ...]
,但这对我来说似乎有些愚蠢; - )