Python:字符串操作的聪明方法
我是Python新手,目前正在阅读“深入Python”中关于字符串操作的一章 我想知道做以下事情最好(或最聪明/最有创意)的方法是什么: 1) 从这个字符串中摘录:“stackoverflow.com/questions/ask”这个词“questions.”我做了string.split(/)[0]——但这不是很聪明 2) 查找给定数字或字符串中最长的回文 3) 从一个给定的单词(即“猫”)开始——找到所有可能的方法,从这个单词到另一个三个字母的单词(“狗”),一次改变一个字母,使每个字母的变化形成一个新的、有效的单词 例如:猫、床、点、狗 如果字符串是Python:字符串操作的聪明方法,python,string,Python,String,我是Python新手,目前正在阅读“深入Python”中关于字符串操作的一章 我想知道做以下事情最好(或最聪明/最有创意)的方法是什么: 1) 从这个字符串中摘录:“stackoverflow.com/questions/ask”这个词“questions.”我做了string.split(/)[0]——但这不是很聪明 2) 查找给定数字或字符串中最长的回文 3) 从一个给定的单词(即“猫”)开始——找到所有可能的方法,从这个单词到另一个三个字母的单词(“狗”),一次改变一个字母,使每个字母的变
s
:
max((j-i,s[i:j])对于范围内的i(len(s)-1)对于范围内的j(i+2,len(s)+1),如果s[i:j]==s[j-1:i-1:-1])[1]
将返回答案。对于作为个人练习的#2-,这里是给您的,(希望)注释良好的代码,并提供一些提示
#!/usr/bin/env python2
# Let's take this string:
a = "palindnilddafa"
# I surround with a try/catch block, explanation following
try:
# In this loop I go from length of a minus 1 to 0.
# range can take 3 params: start, end, increment
# This way I start from the thow longest subsring,
# the one without the first char and without the last
# and go on this way
for i in range(len(a)-1, 0, -1):
# In this loop I want to know how many
# Palidnrome of i length I can do, that
# is len(a) - i, and I take all
# I start from the end to find the largest first
for j in range(len(a) - i):
# this is a little triky.
# string[start:end] is the slice operator
# as string are like arrays (but unmutable).
# So I take from j to j+i, all the offsets
# The result of "foo"[1:3] is "oo", to be clear.
# with string[::-1] you take all elements but in the
# reverse order
# The check string1 in string2 checks if string1 is a
# substring of string2
if a[j:j+i][::-1] in a:
# If it is I cannot break, 'couse I'll go on on the first
# cycle, so I rise an exception passing as argument the substring
# found
raise Exception(a[j:j+i][::-1])
# And then I catch the exception, carrying the message
# Which is the palindrome, and I print some info
except Exception as e:
# You can pass many things comma-separated to print (this is python2!)
print e, "is the longest palindrome of", a
# Or you can use printf formatting style
print "It's %d long and start from %d" % (len(str(e)), a.index(str(e)))
在讨论之后,如果事情不顺利,我有点抱歉。我已经编写了回文搜索器的另一个实现,如果sberry2A可以,我想知道一些基准测试的结果
请注意,指针和难处理的“+1-1”问题(我想)有很多错误,但想法很清楚。从中间开始,然后展开,直到可以
代码如下:
#!/usr/bin/env python2
def check(s, i):
mid = s[i]
j = 1
try:
while s[i-j] == s[i+j]:
j += 1
except:
pass
return s[i-j+1:i+j]
def do_all(a):
pals = []
mlen = 0
for i in range(len(a)/2):
#print "check for", i
left = check(a, len(a)/2 + i)
mlen = max(mlen, len(left))
pals.append(left)
right = check(a, len(a)/2 - i)
mlen = max(mlen, len(right))
pals.append(right)
if mlen > max(2, i*2-1):
return left if len(left) > len(right) else right
string = "palindnilddafa"
print do_all(string)
这是三个独立的问题,没有任何共同之处。总的来说,你似乎没有什么问题。问题是他对这门语言还不熟悉,而且已经在寻找“最疯狂的方式”:-D。。顺便说一句,为什么string.split(“/”)[1]不聪明?@parse舌头你已经有了什么?@SilentGhost——以上三个问题都是我分配给自己学习字符串的项目。老实说,我使用的文本不是很有启发性,StackOverflow上的人总是发表创造性的问题解决方案。我希望能从这些解决方案中获得一些见解,以便更多地了解Python。在我看来,聪明/疯狂与最佳并不相同。我完全理解这个答案的0%。太棒了。但是它失败了——如果你用字符串“racecar”,它会返回“aceca”哇!谢谢。这非常有用。我得好好学习了。我已经试着把我能写的东西都写下来了,还有很多东西。很乐意帮忙。回答得好。大约比我的解决方案快5倍(这肯定不是最佳解决方案),但仍然比@kefeizhou的解决方案慢2倍。@sberry2A,我已经编写了另一个实现,您能看一下吗?