如果我想将列表初始化为5个零,那就非常简单了:
[0] * 5
但是,如果我更改我的代码以放置更复杂的数据结构,如零列表:
[[0]] * 5
将不会按预期工作,因为它将是同一列表的10个副本。我必须这样做:
[[0] for i in xrange(5)]
感觉笨重并且使用变量,所以有时我甚至会这样做:
[[0] for _ in " "]
但是如果我想要一份零列表,那就更加丑陋了:
[[[0] for _ in " "] for _ in " "]
所有这些而不是我想做的事情:
[[[0]]*5]*5
有没有人找到一种处理这个“问题”的优雅方式?
答案 0 :(得分:9)
在考虑了一下之后,我提出了这个解决方案:(没有导入的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 ^^)
测试给出以下结果:
>>> 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]]]
另一种解决方案,其优点是使用“[[[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
测试:
# 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]]]
顺便说一句。 numpy库有一个np.zeros(s)
- 函数,其中s
的形状类似于(3,4,5)
>>> s = (2,2)
>>> np.zeros(s)
array([[ 0., 0.],
[ 0., 0.]])
最后进行性能测试:
# 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
我发现发现这个问题真的很有趣。所以,谢谢你的问题:)
答案 1 :(得分:5)
如果我经常要求列表列表......我只需将其建筑物打包成一个小型工厂功能,例如:
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)
在实践中,我甚至都不打扰,因为我对这种多维列表的使用很少,所以内联列表理解就好了。然而,建立你自己的小实用函数模块的一般想法,你需要经常执行的简单任务(在你看来)并不是通过内联习语优雅地完成,这是唯一的方法! - )
答案 2 :(得分:2)
另一个是扩展列表类:
import copy
class mlist(list):
def __mul__(self, n):
res = mlist()
for _ in xrange(n):
for l in self:
res.append(copy.deepcopy(l))
return res
然后:
>>> hey = mlist([mlist([0])])
>>> hey
[[0]]
>>> hey * 4
[[0], [0], [0], [0]]
>>> blah = hey * 4
>>> blah[0][0] = 9
>>> blah
[[9], [0], [0], [0]]
但初始化mlist
很烦人。
答案 3 :(得分:0)
一种解决方案是拥有辅助功能:
import copy
def r(i,n):
return [copy.deepcopy(i) for _ in xrange(n)]
然后:
r([0],5)
r(r([0],5),5)
但这种语法很难看。