将嵌套的 Python 循环转换为列表推导式
我已经开始解决一些 Project Euler 问题,并解决了 number 4 使用简单的强力解决方案:
def mprods(a,b):
c = range(a,b)
f = []
for d in c:
for e in c:
f.append(d*e)
return f
max([z for z in mprods(100,1000) if str(z)==(''.join([str(z)[-i] for i in range(1,len(str(z))+1)]))])
解决后,我尝试使其尽可能紧凑,并得出了那个可怕的底线!
为了不让事情半途而废,我试图将 mprods 函数压缩为列表理解。到目前为止,我已经提出了这些尝试:
[d*e for d,e in (range(a,b), range(a,b))]
显然完全走错了路。 :-)[d*e for x in [e for e in range(1,5)] for d in range(1,5)]
这给了我[4, 8, 12, 16, 4, 8, 12, 16, 4, 8, 12, 16, 4, 8, 12, 16]
,我期望的[1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16]
或类似内容。
有 Python 达人可以提供帮助吗? :)
I've started working on some Project Euler problems, and have solved number 4 with a simple brute force solution:
def mprods(a,b):
c = range(a,b)
f = []
for d in c:
for e in c:
f.append(d*e)
return f
max([z for z in mprods(100,1000) if str(z)==(''.join([str(z)[-i] for i in range(1,len(str(z))+1)]))])
After solving, I tried to make it as compact as possible, and came up with that horrible bottom line!
Not to leave something half-done, I am trying to condense the mprods
function into a list comprehension. So far, I've come up with these attempts:
[d*e for d,e in (range(a,b), range(a,b))]
Obviously completely on the wrong track. :-)[d*e for x in [e for e in range(1,5)] for d in range(1,5)]
This gives me[4, 8, 12, 16, 4, 8, 12, 16, 4, 8, 12, 16, 4, 8, 12, 16]
, where I expect[1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16]
or similar.
Any Pythonistas out there that can help? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
xrange(900,1000)
与range(900,1000)
类似,但它不返回列表,而是返回一个根据需要生成范围内数字的对象。对于循环,这比 range() 稍快,并且内存效率更高。product(xrange(900,1000),xrange(900,1000))
给出输入可迭代对象的笛卡尔积。它相当于嵌套的 for 循环。例如,product(A, B)
返回的结果与:((x,y) for x in A for y in B)
相同。最左边的迭代器位于最外面的 for 循环中,因此输出元组以类似于里程表的方式循环(最右边的元素在每次迭代时都会发生变化)。product('ab', range(3))
-->('a',0) ('a',1) ('a',2) ('b',0) ('b',1) ('b',2)
产品((0,1), (0,1), (0,1))
-->(0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) ...
str(i)[::-1]
是用于反转列表的列表切片简写。注意所有内容是如何包装在 生成器表达式 中的,一个高列表推导式和生成器的性能、内存高效泛化。
另请注意,由两个 2 位数字的乘积组成的最大回文数是由数字 91 99 组成的,这两个数字在
范围 (90,100)
中。外推到 3 位数字时,您可以使用range(900,1000)
。xrange(900,1000)
is likerange(900,1000)
but instead of returning a list it returns an object that generates the numbers in the range on demand. For looping, this is slightly faster than range() and more memory efficient.product(xrange(900,1000),xrange(900,1000))
gives the Cartesian product of the input iterables. It is equivalent to nested for-loops. For example,product(A, B)
returns the same as:((x,y) for x in A for y in B)
. The leftmost iterators are in the outermost for-loop, so the output tuples cycle in a manner similar to an odometer (with the rightmost element changing on every iteration).product('ab', range(3))
-->('a',0) ('a',1) ('a',2) ('b',0) ('b',1) ('b',2)
product((0,1), (0,1), (0,1))
-->(0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) ...
str(i)[::-1]
is list slicing shorthand to reverse a list.Note how everything is wrapped in a generator expression, a high performance, memory efficient generalization of list comprehensions and generators.
Also note that the largest palindrome made from the product of two 2-digit numbers is made from the numbers 91 99, two numbers in the
range(90,100)
. Extrapolating to 3-digit numbers you can userange(900,1000)
.我想你会喜欢这句话(为了便于阅读而格式化):
或者稍微改变一下:
想知道 Lisp 中有多少个括号......
I think you'll like this one-liner (formatted for readability):
Or slightly changed:
Wonder how many parens that would be in Lisp...