第一个脚本,请评论和建议 [英] First script, please comment and advise

查看:83
本文介绍了第一个脚本,请评论和建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这不是非常pythonic;意见和建议赞赏

def curious(text):

"""返回加密的输入文本中的单词,除了第一个和最后一个字母。 """

new_text =""

word =""

for ch in text:

如果ch inabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ:

word = word + ch

else:

new_text = new_text + scramble (字)

word =""

new_text = new_text + ch

返回new_text


def scramble(word):

"""来自随机导入randint的争夺词">


如果len(word)< b
4:

返回单词

new_word = word [0]


### transform" curious"进入[''你',''r'',''我',''o'',''你']

letters = []

for ch in word:

letters.append(ch)

del letters [0:1]

del letters [-1]


###为什么没有范围(len(字母) - 1,0,-1)工作?

for i in range(len (字母) - 1,-1,-1):

j = randint(0,i)

new_word = new_word + letters [j]

del letters [j]

返回new_word + word [-1]


打印好奇(好奇.__ doc__)

-

如果您通过Google发帖,请阅读< http://cfaj.freeshell.org/google>

解决方案

在文章< sl ******************* @ ID-203069.user.individual.net>,

Pedro Graca <他**** @ dodgeit.com>写道:

我确定这不是非常pythonic;意见和建议赞赏

def curious(text):
"""返回加密的输入文本中的单词,除了第一个和最后一个字母。 """
new_text =""
word =""
for ch in text:
如果ch inabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ:
word = word + ch
else:
new_text = new_text + scramble(word)
word =""
new_text = new_text + ch
return new_text

def scramble(word):
"""来自随机导入randint的争夺词"

如果len(word)< 4:
返回单词
new_word = word [0]

### transform" curious"进入[''你',''r'',''我',''o'',''你']
字母= []
for ch in word:< br。> letters.append(ch)
del letters [0:1]
del letters [-1]

###为什么没有范围(len(字母) - 1,0,-1)工作?
我在范围内(len(字母) - 1,-1,-1):
j = randint(0,i)
new_word = new_word + letters [j]
del letters [j]
返回new_word + word [-1]

打印好奇(好奇.__ doc __)




def curious(text):

"""返回加密的输入文本中的单词,除了

的第一个和最后一个字母。 """

new_text =""

word =""

for ch in text:

如果ch.isalpha():

word + = ch

else:

new_text + = scramble(word)

word =""

new_text + = ch

new_text + = scramble(word)

返回new_text


def scramble(单词):

"""来自随机导入shuffle的争夺词">



如果len(word)< 4:

返回单词

letters = list(word [1:-1])

shuffle(字母)

返回单词[0] +"" .join(字母)+单词[-1]


只需


我的版本类似于Just one:


来自随机导入shuffle


def scramble_text(text):

"""返回输入文本字符串中的单词,加上

除了第一个和最后一个字母。""

def scramble_word(word) :

if len(word)< 4:返回单词

core = list(单词[1:-1])

shuffle(核心)

返回单词[0] + " .join(核心)+字[-1]


返回" " .join(map(scramble_word,text.split()))

print scramble_text(scramble_text .__ doc__)

在文章< 11 ********************** @ p10g2000cwp.googlegroups .com> ;,
是************ @ lycos.com 写道:

我的版本类似于Just one:

来自随机导入shuffle

def scramble_text(text):
"""返回输入文本字符串中的单词加扰
除了第一个和最后一个字母。""
def scramble_word(单词):
如果len(单词)< 4:返回单词
core = list(word [1:-1])
shuffle(core)
返回单词[0] +"" .join(core)+ word [ -1]

返回" " .join(map(scramble_word,text.split()))

print scramble_text(scramble_text .__ doc __)




你的文字分裂表现与原作完全不同;我认为

是故意的,没有任何影响。


顺便说一句。我发现在这里使用嵌套函数完全是假的:你

不需要周围的范围。


只是


I''m sure this isn''t very pythonic; comments and advice appreciated
def curious(text):
""" Return the words in input text scrambled except for the first and last letter. """
new_text = ""
word = ""
for ch in text:
if ch in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ":
word = word + ch
else:
new_text = new_text + scramble(word)
word = ""
new_text = new_text + ch
return new_text

def scramble(word):
""" scramble word """
from random import randint

if len(word) < 4:
return word
new_word = word[0]

### transform "curious" into [''u'', ''r'', ''i'', ''o'', ''u'']
letters = []
for ch in word:
letters.append(ch)
del letters[0:1]
del letters[-1]

### why doesn''t range(len(letters) - 1, 0, -1) work?
for i in range(len(letters) - 1, -1, -1):
j = randint(0, i)
new_word = new_word + letters[j]
del letters[j]
return new_word + word[-1]

print curious(curious.__doc__)
--
If you''re posting through Google read <http://cfaj.freeshell.org/google>

解决方案

In article <sl*******************@ID-203069.user.individual.net>,
Pedro Graca <he****@dodgeit.com> wrote:

I''m sure this isn''t very pythonic; comments and advice appreciated
def curious(text):
""" Return the words in input text scrambled except for the first and
last letter. """
new_text = ""
word = ""
for ch in text:
if ch in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ":
word = word + ch
else:
new_text = new_text + scramble(word)
word = ""
new_text = new_text + ch
return new_text

def scramble(word):
""" scramble word """
from random import randint

if len(word) < 4:
return word
new_word = word[0]

### transform "curious" into [''u'', ''r'', ''i'', ''o'', ''u'']
letters = []
for ch in word:
letters.append(ch)
del letters[0:1]
del letters[-1]

### why doesn''t range(len(letters) - 1, 0, -1) work?
for i in range(len(letters) - 1, -1, -1):
j = randint(0, i)
new_word = new_word + letters[j]
del letters[j]
return new_word + word[-1]

print curious(curious.__doc__)



def curious(text):
""" Return the words in input text scrambled except for the
first and last letter. """
new_text = ""
word = ""
for ch in text:
if ch.isalpha():
word += ch
else:
new_text += scramble(word)
word = ""
new_text += ch
new_text += scramble(word)
return new_text

def scramble(word):
""" scramble word """
from random import shuffle
if len(word) < 4:
return word
letters = list(word[1:-1])
shuffle(letters)
return word[0] + "".join(letters) + word[-1]

Just


My version is similar to Just one:

from random import shuffle

def scramble_text(text):
"""Return the words in input text string scrambled
except for the first and last letter."""
def scramble_word(word):
if len(word) < 4: return word
core = list(word[1:-1])
shuffle(core)
return word[0] + "".join(core) + word[-1]

return " ".join(map(scramble_word, text.split()))

print scramble_text(scramble_text.__doc__)


In article <11**********************@p10g2000cwp.googlegroups .com>,
be************@lycos.com wrote:

My version is similar to Just one:

from random import shuffle

def scramble_text(text):
"""Return the words in input text string scrambled
except for the first and last letter."""
def scramble_word(word):
if len(word) < 4: return word
core = list(word[1:-1])
shuffle(core)
return word[0] + "".join(core) + word[-1]

return " ".join(map(scramble_word, text.split()))

print scramble_text(scramble_text.__doc__)



Your text split behaves completely different from the original; I think
it was intentional that any puntuation wasn''t affected.

Btw. I find the use of a nested function here completely bogus: you
don''t need the surrounding scope.

Just


这篇关于第一个脚本,请评论和建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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