反转或反转一个字符串...警告这是一个咆哮 [英] invert or reverse a string... warning this is a rant

查看:81
本文介绍了反转或反转一个字符串...警告这是一个咆哮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Python不能像Ruby这样的reverse()函数/方法?

Python:

x =''a_string''

#反转字符串

print x [:: - 1]


Ruby:

x =' 'a_string''

#反转字符串

print x.reverse


Ruby方法对我来说是有道理的存在。 Python

方法对我(作为一个人)来说并不容易记住。这可以改变吗?或者我应该盲目地记住这些东西吗?


P.S.我比90%的时候更喜欢Python,并且使用Python 90%的时间,但是10%的时间,像这样的小事情让我发疯了!

Why can''t Python have a reverse() function/method like Ruby?

Python:
x = ''a_string''
# Reverse the string
print x[::-1]

Ruby:
x = ''a_string''
# Reverse the string
print x.reverse

The Ruby approach makes sense to me as a human being. The Python
approach is not easy for me (as a human being) to remember. Can that be
changed or should I just start blindly memorizing this stuff?

P.S. I like Python better than Ruby 90% of the time and use Python 90%
of the time, but 10% of the time, little things such as this drive me crazy!

推荐答案

rick写道:
rick wrote:

为什么Python没有像Ruby这样的reverse()函数/方法?
Why can''t Python have a reverse() function/method like Ruby?



我在日常编程中没有充足的说法,认为它不是必需的b $ b,但我的问题是为什么呢?你需要扭转字符串吗?它是否经常发生以保证一种方法的东西?

I''m not steeped enough in daily programming to argue that it isn''t
necessary, but my question is why do you need to reverse strings? Is it
something that happens often enough to warrant a method for it?


rick< at ******* @ vt .eduwrites:
rick <at*******@vt.eduwrites:

为什么Python不能像Ruby这样的reverse()函数/方法?


Python:

x =''a_string''

#反转字符串

print x [:: - 1]


Ruby方法对我来说是有意义的。 Python

方法对我(作为一个人)来说并不容易记住。那可以改变吗?或者我应该盲目地记住这些东西吗?
Why can''t Python have a reverse() function/method like Ruby?

Python:
x = ''a_string''
# Reverse the string
print x[::-1]

The Ruby approach makes sense to me as a human being. The Python
approach is not easy for me (as a human being) to remember. Can that
be changed or should I just start blindly memorizing this stuff?



您可以使用:


print''''。join(reverse(x))


这看起来有点奇怪,但它直接结合了着名的Python

成语。

You could use:

print ''''.join(reversed(x))

That also looks a little bit weird, but it combines well-known Python
idioms straightforwardly.


rick写道:
rick wrote:

Ruby方法对我来说是有意义的。
The Ruby approach makes sense to me as a human being.



地球上的人类会花很多时间来换弦吗?

这绝对不是很常见的事情在这里。


无论如何,如果你这么做,为什么不定义一个辅助函数?


def reverse(s):

返回s [:: - 1]


打印反向(redael ruoy ot em ekat)


< / F>

do the humans on your planet spend a lot of time reversing strings?
it''s definitely not a very common thing to do over here.

anyway, if you do this a lot, why not define a helper function?

def reverse(s):
return s[::-1]

print reverse("redael ruoy ot em ekat")

</F>


这篇关于反转或反转一个字符串...警告这是一个咆哮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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