简体   繁体   English

在Python中创建数学系列的高效,优雅方法

[英]Efficient, elegant way to create mathematical series in Python

In Javascript, here is a simple mathematical series. 在Javascript中,这是一个简单的数学系列。

var myList = []

for (var x = 2; x < 10000; x = x * 2) {
    mylist.push(x);
}

I cannot create a similar series in Python without: 如果没有以下条件,就无法在Python中创建类似的系列:

  • Using a while loop 使用while循环

  • Looping over the entire range of numbers to 10,000 in a comprehension 全面理解数字范围至10,000

Is this one of the rare cases where Python is a less elegant language? 这是Python是一种不太优雅的语言的罕见情况之一吗?
Am I unaware of some functionality in Python here? 我在这里没有意识到Python的某些功能吗?

This can be done with itertools (as suggested by 4castle) operating on generator expressions. 这可以通过对生成器表达式进行操作的itertools(由4castle建议)来完成。

from itertools import takewhile, count
myList = takewhile(lambda x: x < 10000, map(lambda x: 2 ** x, count()))

In python 2, you will need to use itertools.imap instead of map . 在python 2中,您将需要使用itertools.imap而不是map

I don't get the question here. 我在这里没有问题。 What do you class as elegance? 您怎么称呼高雅? For a couple of (perfectly legible) extra you can have a while loop set up. 对于几个(完全清晰)的额外内容,您可以设置一个while循环。

def my_func():
    x = 2
    my_list = []

    while x < 10000:
        my_list.append(x)
        x = x * 2

I used timeit on an ancient laptop for basic code: 我在古老的笔记本电脑上使用timeit来获取基本代码:

10000000 loops, best of 3: 34.9 ns per loop

This debate is irrelevant in terms of execution time or readability. 就执行时间或可读性而言,这种辩论是无关紧要的。 There is no merit in discussing this further than to get into a debate about {} and ; 除了展开有关{};的辩论之外,没有其他必要讨论; that are all over JS in general, which is counter-productive. 通常在JS上都是这样,这适得其反。 I'm sure some people are looking for one-liners but that will be obfuscated and inefficient. 我敢肯定,有些人正在寻找一线客车,但这会造成混淆和效率低下。

"Efficient" and "elegant" can be a bit ambiguous. “高效”和“优雅”可能有点含糊。 I'll assume "efficient" means both concise to type and quick to evaluate. 我假设“有效”是指简洁明了且易于评估。 And elegant I suppose means the result of its evaluation can be quickly and easily understood from the source. 我认为,优雅意味着可以从源头快速,轻松地理解其评估结果。

Your question, in general, is difficult to answer because, for the most part, mathematical series as written by mathematicians can include arbitrarily complex operations that may or may not resolve to something "elegant" in one language or another (eg linear algebra is much more elegant in MATLAB/Octave than in Python). 通常,您的问题很难回答,因为在大多数情况下,由数学家编写的数学级数可以包含任意复杂的运算,这些运算可能会或可能不会解析为一种或多种语言中的“优雅”(例如,线性代数很多)在MATLAB / Octave中比在Python中更优雅)。

Your question, specifically, is very easy to express elegantly (in either language) by noting the actual underlying mathematical expression you're really trying to evaluate: 具体来说,您的问题很容易通过指出您实际上要评估的基本数学表达式来优雅地表达(用两种语言):

在此处输入图片说明

Which, in python, is this: 在python中是这样的:

from math import floor, log2
[2**x for x in range(1, floor(log2(10000))+1)]

In fact, I'm not sure you can represent the construction of this set as elegantly with JavaScript, but don't quote me on that because I am by no means an expert in that language. 实际上,我不确定您是否可以用JavaScript优雅地表示此集合的构造,但不要在此引用我的意思,因为我绝不是该语言的专家。

So the answer is... It really depends on what you're trying to express. 因此,答案是……这实际上取决于您要表达的内容。 In this case, I prefer the python list comprehension to evaluation in a loop (in either language), but that's just me. 在这种情况下,我更喜欢python列表理解而不是循环评估(使用任何一种语言),但这就是我自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM