了解列表理解与传统循环和构建 [英] Understanding list comprehension vs traditional loop and build
问题描述
我试图巩固我对列表的理解和最佳使用,我遇到了列表理解,并且阅读了很多有关列表的内容,但是却遇到了一个特别棘手的问题.
I'm trying to nail my understanding and best use of Lists, I have come across list comprehension and read plenty about them, but am choking on one particular burning question.
面对这个挑战:
def matrix_mult(m1, m2):
"""
>>> matrix_mult([[1, 2], [3, 4]], [[5, 6], [7, 8]])
[[19, 22], [43, 50]]
>>> matrix_mult([[1, 2, 3], [4, 5, 6]], [[7, 8], [9, 1], [2, 3]])
[[31, 19], [85, 55]]
>>> matrix_mult([[7, 8], [9, 1], [2, 3]], [[1, 2, 3], [4, 5, 6]])
[[39, 54, 69], [13, 23, 33], [14, 19, 24]]
"""
我创建了这个解决方案,在我看来,这是最合乎逻辑的,并且与我以前的编程经验相符,我或多或少地输入了我所想的...
I created this solution, to me it seemed most logical and matched my previous programming experience, I more or less typed as i was thinking ...
# 1 using a traditional List buildup method
res = []
for i in range(len(m1)):
sub = []
for j in range(len(m2[0])):
sub.append(row_times_column( m1, i, m2, j ))
res.append(sub)
return res
然后,我找到了具有列表理解"功能的解决方案(我将vars重命名为与我的匹配,以便更好地了解两种解决方案之间的差异:
Then I found this solution which featured 'list comprehension' (I renamed the vars to match mine in order to better grok the diffs between the two solutions:
# 2 using list comprehension
res = [[0] * len(m1) for x in xrange(len(m2[0]))]
for i in range(len(res)):
for j in range(len(res[i])):
res[i][j] = row_times_column(m1, i, m2, j)
return res
第二种解决方案是建立一个与预期答案的形状相匹配的基于零的矩阵,但是这种方法是列表理解"的意思吗?,还是在这里进行更多的工作?
The second solution is building a zero based matrix which matches the shape of the intended answer, but is this method what is meant by "list comprehension", or is there more going on here?
这里是row_times_column()定义,以确保内容完整.
Here is the row_times_column() def, for fullness.
def row_times_column(m1, row, m2, column):
"""
>>> row_times_column([[1, 2], [3, 4]], 0, [[5, 6], [7, 8]], 0)
19
>>> row_times_column([[1, 2], [3, 4]], 0, [[5, 6], [7, 8]], 1)
22
"""
i = 0
for index, value in enumerate(m1[row]):
i += value * m2[index][column]
return i
我怀疑有第三种(以及更多种)使用lambda的方法来解决此问题,但我想我想先对这2种方法发表评论.
I suspect there is a third (and many more) way of solving this, using a lambda, but I thought I'd ask for comment on these 2 first.
示例取自 http://openbookproject.net/thinkcs/python/english2e/ch09 .html
编辑 现在对列表理解有了更好的处理,这里给出答案.
EDIT Got a much better handle on list comprehension now, thx to answers here given.
还是,任何人都可以解释创建空白矩阵并放置正确答案而不是仅仅创建新列表的逻辑吗?
Still, can anyone explain the logic of creating a blank matrix into which the correct answers are placed vs just creating a new list?
推荐答案
列表理解只是基于另一个列表创建列表的一种方法. (或其他可迭代项)
List comprehension is simply a way of creating a list based on another list. (Or other iterable item)
例如,如果我们有一个列表a = [1, 2, 5, 7]
,那么我们可以创建一个列表b
,其中包含以两种方式加倍的a
值.
For instance, if we have a list a = [1, 2, 5, 7]
, then we could create a list, b
, containing the values of a
doubled in two ways.
b = []
for e in a:
b.append(2*e)
具有列表理解力
b = [2*e for e in a]
仅此而已.这只是用于基于列表构建列表的一种不错的语法.
There's nothing more to it than that. It's simply a nice syntax for building lists based on lists.
- Python: List Comprehensions
- List Comprehensions on (An Unofficial) Python Tutorial Wiki
这篇关于了解列表理解与传统循环和构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!