如果我想要一个初始化为5个零的列表,这非常好用且简单:
[0] * 5
Run Code Online (Sandbox Code Playgroud)
但是,如果我更改我的代码以放置更复杂的数据结构,如零列表:
[[0]] * 5
Run Code Online (Sandbox Code Playgroud)
将不会按预期工作,因为它将是同一列表的10个副本.我要做:
[[0] for i in xrange(5)]
Run Code Online (Sandbox Code Playgroud)
感觉笨重并使用变量,所以有时我甚至做:
[[0] for _ in " "]
Run Code Online (Sandbox Code Playgroud)
但是,如果我想要一个零列表列表,它会变得更加丑陋:
[[[0] for _ in " "] for _ in " "]
Run Code Online (Sandbox Code Playgroud)
所有这些而不是我想做的事情:
[[[0]]*5]*5
Run Code Online (Sandbox Code Playgroud)
有没有人找到一种优雅的方式来处理这个"问题"?
在考虑了一下之后,我想出了这个解决方案:(没有导入的7行)
# helper
def cl(n, func):
# return a lambda, that returns a list, where func(tion) is called
return (lambda: [func() for _ in range(n)])
def matrix(base, *ns):
# the grid lambda (at the start it returns the base-element)
grid = lambda: base
# traverse reversed, to handle the midmost values first
for n in reversed(ns):
# assign a new lambda with the last grid within (and call it)
grid = cl(n, grid)
return grid() # call the full grid (but the matrix calls you ^^)
Run Code Online (Sandbox Code Playgroud)
测试给出以下结果:
>>> from pprint import pprint as pp
>>>
>>> matrix(None, 2,3)
[[None, None, None], [None, None, None]]
>>>
>>> matrix(None, 4,3)
[[None, None, None], [None, None, None], [None, None, None], [None, None, None]]
>>>
>>> x = matrix(None, 3,5,2)
>>> pp(x)
[[[None, None], [None, None], [None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None], [None, None], [None, None]]]
>>> x[1][3][0] = "test"
>>> pp(x)
[[[None, None], [None, None], [None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None], ['test', None], [None, None]],
[[None, None], [None, None], [None, None], [None, None], [None, None]]]
Run Code Online (Sandbox Code Playgroud)
另一种解决方案,其优点是使用"[[[0]]*5]*5"-syntax:
def uniq(base, l):
# function used to replace all values with the base
nl = []
for i in l:
if type(i) is list:
nl.append(uniq(base, i)) # recursion for deep lists
else:
nl.append(base)
return nl
Run Code Online (Sandbox Code Playgroud)
测试:
# first arg is the base, the 0 inside the [] is just a dummy
# (for what None is the best choice usually)
>>> x = uniq(0, [[[0]]*5]*5)
>>> x[0][3][0] = 5
>>> pp(x)
[[[0], [0], [0], [5], [0]],
[[0], [0], [0], [0], [0]],
[[0], [0], [0], [0], [0]],
[[0], [0], [0], [0], [0]],
[[0], [0], [0], [0], [0]]]
Run Code Online (Sandbox Code Playgroud)
顺便说一句.numpy库有一个np.zeros(s)函数,s其形状就像(3,4,5)
>>> s = (2,2)
>>> np.zeros(s)
array([[ 0., 0.],
[ 0., 0.]])
Run Code Online (Sandbox Code Playgroud)
最后进行性能测试:
# functions are already defined ...
import timeit
>>> # Alex Martelli's Code
>>> t1 = timeit.Timer( lambda: multi_dimension_list(None, 3,4,5) )
>>> # the two mentioned above
>>> t2 = timeit.Timer( lambda: matrix(None, 3,4,5) )
>>> t3 = timeit.Timer( lambda: uniq(None, [[[None]*5]*4]*3) )
>>>
>>> t1.timeit(10000)
2.1910018920898438
>>> t2.timeit(10000)
0.44953203201293945
>>> t3.timeit(10000)
0.48807907104492188
Run Code Online (Sandbox Code Playgroud)
我发现发现这个问题真的很有趣.所以,谢谢你的问题:)
如果我经常要求列表列表......我只需将其构建包装成一个小工厂函数,例如:
import copy
def multi_dimension_list(baseitem, *dimensions):
dimensions = list(dimensions)
result = [baseitem] * dimensions.pop(-1)
for d in reversed(dimensions):
result = [copy.deepcopy(result) for _ in range(d)]
return result
eg = multi_dimension_list(0, 3, 4, 5)
print(eg)
# and just to prove the parts are independent...:
eg[1][1][1] = 23
print(eg)
Run Code Online (Sandbox Code Playgroud)
在实践中,我甚至都不打扰,因为我对这种多维列表的使用很少,所以内联列表理解就好了.然而,建立你自己的小实用函数模块的一般想法,你需要经常执行的简单任务(在你看来)并不是通过内联习语优雅地完成,这是唯一的方法! - )
| 归档时间: |
|
| 查看次数: |
2341 次 |
| 最近记录: |