用Python编写的小新手程序 [英] Little novice program written in Python

查看:73
本文介绍了用Python编写的小新手程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部。


我只是踩着Python,而且对于初学者来说,我正在编写一些

基本数论算法(是的,我知道它们中的大多数已经作为模块实现了
,但这是学习语言习语的练习)。


正如你从下面的代码中看到的那样,我的背景是在C中,没有太多

复杂性。


我想要的是接受一些批评我的代码使它更多

Python''esque,并且可能以更有效的方式使用计算机的资源(下面实现的算法是Sieve of Eratosthenes):


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#!/ usr / bin / env python

n = int(raw_input())

a = [i for i in range(0, n + 1)]

a [1] = 0#不是素数

prime = 1#last used prime

finished = False


while(未完成):

prime = prime + 1

#找到新的$

而prime * prime< = n和a [prime] == 0:

prime + = 1

#cross复合数

如果prime * prime< = n:

j = 2 * prime

而j< = n:

a [j] = 0

j + = prime

else:

finished = True


#打印出素数

i = 2

而i< = n:

如果a [i]!= 0:

打印一个[i]

i + = 1

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


感谢您对改进此计划的任何帮助,


-
$ b $bRogérioBrito:rbrito @ {mackenzie,ime.usp} .br:GPG密钥1024D / 7C2CAEB8
http://www.ime .usp.br / ~brito http://meusite.mackenzie。 com.br/r brito

项目:algorithms.berlios.de:lame.sf.net:vrms.alioth.debian.org

解决方案

我想要的是接受一些批评我的代码使其更多


Python''esque,并且可能使用的资源计算机以更有效的方式运行(下面实现的算法是Eratosthenes的Sieve):



它看起来像直接的代码和现在很好。

如果你想稍微调整它,你可以避免使用像

完成的标志。使用break语句。

Raymond


RogérioBrito写道:

$ b $全部。


我只是在Python上沾上脚步,而且对于初学者来说,我只是编码一些基本数字的b $ b理论算法(是的,我知道大多数它们已经作为模块实现了,但这是一个学习语言习语的练习。


正如你从下面的代码中看到的那样,我的背景是在C中,没有太多

非常复杂。


我想要什么就像是要接受一些批评我的代码来使它成为更多Python''esque并且可能以更有效的方式使用计算机的资源(下面实现的算法是Sieve of

Eratosthenes):


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#!/ usr / bin / env python


n = int(raw_输入())

a = [i for i in range(0,n + 1)]

a [1] = 0#not a prime

prime = 1#last used prime

finished = False


while(not finished):

prime = prime + 1

#找到新的素数

而素数*素数< = n和a [素数] == 0:

素数+ = 1

#越过复合数字

如果素数*素数< = n:

j = 2 * prime

而j< = n:

a [j] = 0

j + = prime

else:

完成=正确


#打印出素数

i = 2

而i< = n:

如果a [i]!= 0:

打印一个[i]

i + = 1

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -


感谢您在改进此计划方面的任何帮助,



你的Python实际上非常好 - 如果Raymond Hettinger代词好吧

好​​吧,然后很少有人会不同意。


至于你的英语,你所寻找的词是Pythonic。 (不是

,你会在韦伯斯特的字典中找到这样一个词)。建议

,你的代码是Pythonesque意味着你发现它是荒谬的或

荒谬(如Monty Python草图),显然不是。


您可能会考虑的另一个问题就是打印出素数,因为它们是生成的,而不是在单独的循环中打印,

虽然是否这种方法在现实生活中更为可取。当然,

取决于应用程序。


问候

Steve


PS:我认为我的邮件或者你的邮件都损坏了缩进。

-

Steve Holden +1 571 484 6266 +1 800 494 3119

Holden Web LLC http://www.holdenweb.com/


4月24日,11:09 * pm,Dennis Lee Bieber< wlfr ... @ ix.netcom.comwrote:


On Thu,2008年4月24日21:31:15 -0300,RogérioBrito< rbr ... @ ime.usp.br>

在comp中声明如下.lang.python:


a = [i for i in range(0,n + 1)]



* * * *嗯......至少在2.4及更早版本中,range()会返回一个列表....没有

需要那个时代的list-comp ...范围( )也从0开始


> n = 5
a = range(n + 1)
a



[0,1,2,3,4,5]


* * * *所以只需


* * * * a =范围(n + 1)


可以使用。当然,如果使用一个版本,其中range()和xrange()

已统一...


> c = list(xrange(n + 1))
c



[0,1,2,3, 4,5]


-

* * * * Wulfraed * * * * Dennis Lee Bieber * * * * * * * KD6MOG

* * * * wlfr ... @ ix.netcom.com * * * * * * * wulfr ... @ bestiaria.com

* * * * * * * * < a rel =nofollowhref =HTTP://wlfraed.home.netcom.com/target =_ blank> HTTP://wlfraed.home.netcom.com/

* * * *(Bestiaria支持人员:* * * * * * * web-a ... @ bestiaria.com)

* * * * * * * * HTTP://www.bestiaria.com/



你'说硬件原生,哪些机器不保证。

Python可以在机器兼容性的另一个方面。堆栈是硬件本机的
,数组的位置不是。 Python可以在更高的维度上检索你的堆栈。


幸运的是,Python的社区坚固反对反生产力

en masse ,所以发型可以。虽然掩盖了

改进的功能,但您可能需要进行贝叶斯网络改变和

课程。社区也重视机器的灵活性 -

独立性。


然而,实数不是整数,所以意义质量为整数

算法可能有利于C.但是你只需要微观销售(和缩放!)

来检查Python的未来。欢迎来到我们的小组。


Hi, All.

I''m just getting my feet wet on Python and, just for starters, I''m coding some
elementary number theory algorithms (yes, I know that most of them are already
implemented as modules, but this is an exercise in learning the language idioms).

As you can see from the code below, my background is in C, without too much
sophistication.

What I would like is to receive some criticism to my code to make it more
Python''esque and, possibly, use the resources of the computer in a more
efficient way (the algorithm implemented below is the Sieve of Eratosthenes):

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#!/usr/bin/env python

n = int(raw_input())
a = [i for i in range(0,n+1)]
a[1] = 0 # not a prime
prime = 1 # last used prime
finished = False

while (not finished):
prime = prime + 1
# find new prime
while prime*prime <= n and a[prime] == 0:
prime += 1
# cross the composite numbers
if prime*prime <= n:
j = 2*prime
while j <= n:
a[j] = 0
j += prime
else:
finished = True

# print out the prime numbers
i = 2
while i <= n:
if a[i] != 0:
print a[i]
i += 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Thank you for any help in improving this program,

--
Rogério Brito : rbrito@{mackenzie,ime.usp}.br : GPG key 1024D/7C2CAEB8
http://www.ime.usp.br/~rbrito : http://meusite.mackenzie.com.br/rbrito
Projects: algorithms.berlios.de : lame.sf.net : vrms.alioth.debian.org

解决方案

What I would like is to receive some criticism to my code to make it more

Python''esque and, possibly, use the resources of the computer in a more
efficient way (the algorithm implemented below is the Sieve of Eratosthenes):

It looks like straight-forward code and is fine as it stands.
If you want to tweak it a bit, you can avoid using a flag like
"finished" by using a break-statement.
Raymond


Rogério Brito wrote:

Hi, All.

I''m just getting my feet wet on Python and, just for starters, I''m
coding some elementary number theory algorithms (yes, I know that most
of them are already implemented as modules, but this is an exercise in
learning the language idioms).

As you can see from the code below, my background is in C, without too
much sophistication.

What I would like is to receive some criticism to my code to make it
more Python''esque and, possibly, use the resources of the computer in a
more efficient way (the algorithm implemented below is the Sieve of
Eratosthenes):

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#!/usr/bin/env python

n = int(raw_input())
a = [i for i in range(0,n+1)]
a[1] = 0 # not a prime
prime = 1 # last used prime
finished = False

while (not finished):
prime = prime + 1
# find new prime
while prime*prime <= n and a[prime] == 0:
prime += 1
# cross the composite numbers
if prime*prime <= n:
j = 2*prime
while j <= n:
a[j] = 0
j += prime
else:
finished = True

# print out the prime numbers
i = 2
while i <= n:
if a[i] != 0:
print a[i]
i += 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Thank you for any help in improving this program,

Your Python is actually pretty good - if Raymond Hettinger pronounces it
OK then few would dare to disagree.

As for your English, though, the word you sought was "Pythonic" (not
that you will ever find such a word in Webster''s dictionary). To suggest
that your code is Pythonesque would mean you found it farcical or
ridiculous (like a Monty Python sketch), which it clearly is not.

Another wrinkle you might consider is simply printing the primes out as
they are generated rather than doing the printing in a separate loop,
though whether that approach would be preferable in "real life" would
depend on the application, of course.

regards
Steve

PS: I think either my mailer or yours has mangled the indentation.
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/


On Apr 24, 11:09*pm, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:

On Thu, 24 Apr 2008 21:31:15 -0300, Rogério Brito <rbr...@ime.usp.br>
declaimed the following in comp.lang.python:

a = [i for i in range(0,n+1)]


* * * * Uhm... At least in 2.4 and earlier, range() returns a list.... No
need for the list-comp in that era... range() also begins with 0

>n = 5
a = range(n+1)
a

[0, 1, 2, 3, 4, 5]

* * * * So just

* * * * a = range(n+1)

could be used. Of course, if using a version where range() and xrange()
have been unified...

>c = list(xrange(n+1))
c

[0, 1, 2, 3, 4, 5]

--
* * * * Wulfraed * * * *Dennis Lee Bieber * * * * * * * KD6MOG
* * * * wlfr...@ix.netcom.com * * * * * * *wulfr...@bestiaria.com
* * * * * * * * HTTP://wlfraed.home.netcom.com/
* * * * (Bestiaria Support Staff: * * * * * * * web-a...@bestiaria.com)
* * * * * * * * HTTP://www.bestiaria.com/

You''re talking hardware-native, which machines don''t guarantee.
Python can in another dimension of machine compatibility. Stacks are
hardware native, the location of an array is not. Python can retrieve
your stack in higher dimensions.

Fortunately, Python''s community is sturdy against counterproductivity
en masse, so it''s okay to hairbrain it. Cover features of
improvements, though, and you might get a Bayes Net change to make and
courses to steer. The community values the flexibility of machine-
independency too.

However, real numbers are not integers, so opinion mass of integer
algorithms may favor C. But you just need micro-sales (and scales!)
to examine the future of Python. Welcome to our group.


这篇关于用Python编写的小新手程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆