输入上的平面(一维)元组:
data = ('a','b','c'.....'z');
输出:具有n(比如9)列
的表(两个维度)table = ?what code here?
所以
print table
( ('a','b','c'...), ('k','l','m','n'...), ....)
这是最简短的方法吗?
答案 0 :(得分:7)
如果您发现其余部分太冗长,那么这是简短版本:
n = 9
table = zip(*[iter(data)]*n)
假设您从列表开始:
>>> data = range(1,101)
>>> data
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
99, 100]
为列表创建一个迭代器:
>>> data_iter = iter(data)
现在使用zip
功能拆分列表。注意,这是
有趣的部分!
>>> table = zip(*[data_iter]*9)
>>> pprint.pprint(table)
[(1, 2, 3, 4, 5, 6, 7, 8, 9),
(10, 11, 12, 13, 14, 15, 16, 17, 18),
(19, 20, 21, 22, 23, 24, 25, 26, 27),
(28, 29, 30, 31, 32, 33, 34, 35, 36),
(37, 38, 39, 40, 41, 42, 43, 44, 45),
(46, 47, 48, 49, 50, 51, 52, 53, 54),
(55, 56, 57, 58, 59, 60, 61, 62, 63),
(64, 65, 66, 67, 68, 69, 70, 71, 72),
(73, 74, 75, 76, 77, 78, 79, 80, 81),
(82, 83, 84, 85, 86, 87, 88, 89, 90),
(91, 92, 93, 94, 95, 96, 97, 98, 99)]
表达式`[data_iter] * 9'产生以下列表:
[ data_iter, data_iter, data_iter, data_iter, data_iter,
data_iter, data_iter, data_iter, data_iter ]
表达式*[data_iter]*9
将此转换为zip
的九个参数
功能。这个zip
函数构建了一个元组列表:
返回元组列表,其中每个元组包含第i个元素 来自每个参数序列。返回的列表将被截断 长度与最短参数序列的长度。
因为我们正在使用迭代器,所以每次zip
函数查找新值时,它都会获得序列中的下一个值。如果我们以相同的方式简单地使用列表,则将此与行为进行对比:
>>> table = zip(*[data]*9)
>>> pprint.pprint(table)
[(1, 1, 1, 1, 1, 1, 1, 1, 1),
(2, 2, 2, 2, 2, 2, 2, 2, 2),
(3, 3, 3, 3, 3, 3, 3, 3, 3),
...
(99, 99, 99, 99, 99, 99, 99, 99, 99),
(100, 100, 100, 100, 100, 100, 100, 100, 100)]
更新:我的同事指出,如果你有NumPy可用,你可以 这样做:
>>> data = numpy.array(range(1,101))
>>> data.reshape([10,10])
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
[ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
[ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40],
[ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50],
[ 51, 52, 53, 54, 55, 56, 57, 58, 59, 60],
[ 61, 62, 63, 64, 65, 66, 67, 68, 69, 70],
[ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80],
[ 81, 82, 83, 84, 85, 86, 87, 88, 89, 90],
[ 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]])
答案 1 :(得分:2)
这个怎么样:
table = tuple(data[n:n+9] for n in xrange(0,len(data),9))
返回通过逐步执行data
9个项目而生成的元组。
如果要更改列数,只需更改生成器中的两个9
答案 2 :(得分:0)
我很惊讶这不在itertools
,但似乎不是。这是我的方法:
def segment_list(lst, step):
"""
Segment lst into a list of step-length lists
"""
segments = []
while True:
if len(lst) <= step:
output.append(lst)
break
output.append(lst[0:step])
lst = lst[step+1:]
return segments
答案 3 :(得分:0)
>>> n = 3
>>> data = ('a', 'b', 'c', 'd', 'e', 'f',
... 'g', 'h', 'i', 'j', 'k', 'l',
... 'm', 'n')
>>> table = []
>>> for i in xrange( 0, divmod( len(data), n)[0] + 1):
... table.append( data[i*n:i*n+n] )
>>> print tuple( table )
(('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'l'), ('m', 'n'))
答案 4 :(得分:0)
Daenth,
在larsks(上图)描述的zip方法是在itertools模块文档中描述 - 在食谱中搜索“石斑鱼”: