增加一个字符串 [英] Incrementing a string

查看:90
本文介绍了增加一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将perl用于很多扔掉的脚本;从阅读它开始,我更喜欢Python的原因,但是使用

perl总是更容易,而不是学习python。


现在我正在编写一个我希望保留的小程序,所以我很可能借此机会尝试学习一些Python。我需要

计算机生成的一组简单的字符串标签。我不知道

预付了多少---每个都是由于用户操作而产生的。


在perl我只是发起


$ label =" a" ;;


然后,使用它后做


$标签++;


这样可以方便地逐步完成字母表,然后转到aa,ab,ac,

....

在Python中,我可以使用生成器从a到z:

def gen_alph ():
.... for i in range(97,123):

.... yield chr(i)

.... g = gen_alph()
g.next()
''a''g.next()
''b''g.next()



''c''


但它看起来超越z到aa等等(相对)复杂。


事实上,我似乎不太可能超越z使用我的

应用程序并且当然不会超越zz,这对于b
$ b计划来说并不算太难。但是我讨厌在无论多么合理的情况下建立局限性。


似乎应该有一个更好的方式让我失踪,因为我是

在perl中思考,而不是用Python思考。 :-)


Best,


John Velman

解决方案

< blockquote> label =" a";


然后,在使用它之后做


label ++;


这样可以方便地逐步完成字母表,然后转到aa,ab,ac,

....


在Python中,我可以使用生成器从a到z:

def gen_alph():
.... for i in range(97,123):

.... yield chr(i)

.... g = gen_alph()
g.next()
''a''g.next()
''b''g.next()



''c''


但它看起来超越z到aa等等(相对)复杂。


事实上,我似乎不太可能在使用我的

应用程序时超越z,当然不是超过zz,这对于
计划来说并不难。但是我讨厌在无论多么合理的情况下建立局限性。


似乎应该有一个更好的方式让我失踪,因为我是

在perl中思考,而不是用Python思考。 :-)


Best,

John Velman


John Velman< ve****@cox.net>写道:

但它看起来超越z到aa等等(相对)复杂。

实际上,我似乎不太可能超越z使用我的
应用程序,当然不会超越zz,这对于编程来说并不是太难。但我讨厌建立局限性,无论多么合理。




Python没有内置的功能,所以你必须考虑

Perl如何真正做到这一点,并在Python中实现类似的东西。

是的,它会有点复杂,但Perl实现者

必须处理相同的复杂性,这是一个体面的练习,你可以这样做。


I''ve used perl for a lot of ''throw away'' scripts; I like Python better in
principle, from reading about it, but it was always easier to just use
perl rather than learn python.

Now I''m writing a smallish program that I expect to keep around, so am
taking this opportunity to try to learn some Python. I have a need for
computer generated set of simple string lables. I don''t know how many in
advance---each is produced as a result of a user action.

In perl I simply initiated

$label= "a";

Then, after using it doing

$label++;

This conveniently steps through the alphabet, then goes on to aa, ab,ac,
....

In Python I can get from a to z with a generator as so:

def gen_alph(): .... for i in range(97,123):
.... yield chr(i)
.... g = gen_alph()
g.next() ''a'' g.next() ''b'' g.next()


''c''

But it looks like going beyond z to aa and so on is (relatively) complicated.

In truth, it seems unlikely that I would ever go beyond z in using my
application, and certainly not beyond zz which wouldn''t be too hard to
program. But I hate to build in limitations no matter how reasonable.

It seems like there should be a better way that I''m missing because I''m
thinking in perl, not thinking in Python. :-)

Best,

John Velman

解决方案

label= "a";

Then, after using it doing


label++;

This conveniently steps through the alphabet, then goes on to aa, ab,ac,
....

In Python I can get from a to z with a generator as so:

def gen_alph(): .... for i in range(97,123):
.... yield chr(i)
.... g = gen_alph()
g.next() ''a'' g.next() ''b'' g.next()


''c''

But it looks like going beyond z to aa and so on is (relatively) complicated.

In truth, it seems unlikely that I would ever go beyond z in using my
application, and certainly not beyond zz which wouldn''t be too hard to
program. But I hate to build in limitations no matter how reasonable.

It seems like there should be a better way that I''m missing because I''m
thinking in perl, not thinking in Python. :-)

Best,

John Velman


John Velman <ve****@cox.net> writes:

But it looks like going beyond z to aa and so on is (relatively)
complicated.

In truth, it seems unlikely that I would ever go beyond z in using my
application, and certainly not beyond zz which wouldn''t be too hard to
program. But I hate to build in limitations no matter how reasonable.



Python doesn''t have that feature built in, so you have to think about
how Perl really does it, and implement something similar in Python.
Yes, it will be a little bit complicated, but the Perl implementers
had to deal with the same complexity, and it''s a decent exercise for
you to do the same.


这篇关于增加一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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