蟒蛇的方式? [英] the python way?

查看:81
本文介绍了蟒蛇的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我已经潜伏了一个月的名单,这是我的第一篇文章。我希望这篇文章在这里是合适的,否则,我的道歉。


我对Python有些新鲜,(我正在阅读所有内容)我可以找到的教程,

并阅读了Andre Lessa的开发人员手册。)

我正在尝试学习Python的思维方式以及语法。


我把这段代码放在一起好玩,基于之前的帖子

关于随机化一个单词。

这个将一个单词洗牌,然后拆分元音,然后用辅音内插元音重新组装



(创造合理的声音胡言乱语)

>
代码对我来说似乎有点笨重。我想知道,这是一个有效的办法吗?或者我是否需要更多的东西?


#--------------------------开始代码------------------

"""扰乱一个单词,但会创建可信的乱码""

导入随机

def shuffled(s):

"""争抢单词""

l = list(s)

random.shuffle(l)

return''''。join( l)


def包含(alist,b):

""" ...是列表a中的字母b ...
ret = []

for all in alist:

#print all

if all == b:

返回1

返回0


def newZip(a1,a2):

"""重新组装"

l1 = len(a1)

l2 = len(a2)


longest = [ a1,a2] [l1< l2]

shortest = [a1,a2] [longest == a1]

diff = max(l1,l2)-min(l1 ,l2)

#print最长

seq = len(最长)

ret =""

for j in range(seq):

if len(最长)> 0:

ret = ret + longest.pop()

如果len(最短)> 0:

ret = ret + shortest.pop()

返回ret


def reinterpolate (字):

"""主要功能"""

wlist = shuffled(list(word))

vlist = list(''aeiouy'')#ok,y isn''真的是一个元音,但是...

vees = filter(lambda x:contains(vlist,x),wlist)

cons = filter(lambda x:not(包含(vlist,x)),wlist)

a = list(vees)

b = list(cons)

返回newZip(a,b )


word =" encyclopedia"

print reinterpolate(word)


#----- --------------------------结束代码---------------------- -----


BTW:我只是好奇,是否有更简单的方法来复制粘贴代码

来自解释器的片段,以便它删除了''...''和

''>>> ''来自例子?我正在使用< cntrl-H>现在搜索和替换

哪个有效,但如果有更好的方法,我想知道。


提前感谢,
-J

Hi All,

I''ve been lurking the list for a month and this is my first post. I am
hoping this post is appropriate here, otherwise, my apologies.

I''m somewhat new to Python, (I''m reading all the tutorials I can find,
and have read through Andre Lessa''s Developers Handbook.)
I am trying to learn the Python way of thinking as well as the syntax.

I popped this bit of code together for fun, based on a previous post
regarding randomizing a word.
This shuffles a word, then splits out the vowels and then reassembles
it with the vowels interpolated between consonants.
(To create plausible sounding gibberish)

The code just seems kind of bulky to me. I am wondering, is this an
efficient way to do things, or am I making things harder than
necessary?

#--------------------------begin code------------------
"""scrambles a word, but creates plausable gibberish"""
import random
def shuffled(s):
""" scrambles word"""
l = list(s)
random.shuffle(l)
return ''''.join(l)

def contains(alist,b):
"""...is letter b in list a..."""
ret = []
for all in alist:
#print all
if all==b:
return 1
return 0

def newZip(a1,a2):
""" reassemble """
l1=len(a1)
l2=len(a2)

longest = [a1,a2][l1<l2]
shortest = [a1,a2][longest == a1]
diff = max(l1,l2)-min(l1,l2)
#print longest
seq = len(longest)
ret = ""
for j in range(seq):
if len(longest)>0:
ret = ret + longest.pop()
if len(shortest)>0:
ret = ret + shortest.pop()
return ret

def reinterpolate(word):
""" main function """
wlist = shuffled(list(word))
vlist = list(''aeiouy'') # ok, y isn''t really a vowel, but...
vees = filter(lambda x: contains(vlist,x),wlist)
cons = filter(lambda x: not(contains(vlist,x)),wlist)
a=list(vees)
b=list(cons)
return newZip(a,b)

word = "encyclopedia"
print reinterpolate(word)

#-------------------------------end code---------------------------

BTW: I''m just curious, is there an easier way to copy-paste code
snippets from the interpreter so that it removes the ''... '' and the
''>>> '' from the examples? I''m using <cntrl-H> search and replace now
which works, but if there is a better way, I''d like to know.

thanks in advance,
-J

推荐答案

Grooooops写道:
Grooooops wrote:
代码似乎有点笨重对我来说。我想知道,这是一种有效的做事方式,还是我把事情变得更加艰难?


比必要的更难。

包含()根本不需要,你可以直接测试''b in alist''。

列表推导简化了reinterpolate(),你有很多冗余的list()调用。


这是一个较短的版本:


"""扰乱一个字,但创造可信的乱码"

导入随机

def shuffled(s):

"""争抢单词""

l = list(s)

random.shuffle(l)

return''''。join( l)

def newZip(a1,a2):

"""重组""

l1 = len(a1)

l2 = len(a2)


最长,最短= [[a1,a2],[a2,a1]] [l1< l2]

diff = max(l1,l2)-min(l1,l2)


seq = len(最长)

ret =""

for j in range(seq):

if len (最长)> 0:

ret + = longest.pop()

如果len(最短)> 0:

ret + = shortest.pop()

返回


def reinterpolate(word):

"""主要功能"""

wlist = shuffled(word)

vlist =''aeiouy''#ok,y不是真正的元音,但是...

vees = [如果x在vlist中,则x为x列表中的x]

cons = [如果x不在vlist中,则x为x列表中的x]

返回newZip(vees,cons)


word =" encyclopedia"

print reinterpolate(word)


Kent
#--------------------------开始代码------------ ------
"""扰乱一个词,但创造了可信的乱码"
导入随机
def shuffled(s):
" ;"" scrambles word"
l = list(s)
random.shuffle(l)
return''''。join(l)

def contains(alist,b):
""" ... is list b in list a ...""
ret = []
for all in alist:
#print all
如果全部== b:
返回1
返回0
def newZip(a1,a2):
"""重新组装"
l1 = len(a1)
l2 = len(a2)

最长= [a1,a2] [l1< l2] shortest = [a1,a2] [longest == a1]
diff = max(l1,l2)-min(l1,l2)
#print longest
seq = len(最长)
ret =""
j在范围内(seq):
如果len(最长)> 0:
ret = ret + longest.pop()
如果len(最短)> 0:
ret = ret + shortest.pop()
返回ret

" ""主要功能""
wlist = shuffled(list(word))
vlist = list(''aeiouy'')#ok,y不是真正的元音,但是.. 。
vees = filter(lambda x:contains(vlist,x),wlist)
cons = filter(lambda x:not(contains(vlist,x)),wlist)
a =列表(vees)
b =列表(缺点)
返回newZip(a,b)

word =" encyclopedia"
print reinterpolate(word)

#-------------------------------结束代码---------- -----------------
The code just seems kind of bulky to me. I am wondering, is this an
efficient way to do things, or am I making things harder than
necessary?
Harder than necessary.
contains() is not needed at all, you can test for ''b in alist'' directly.
List comprehensions simplify reinterpolate(), and you have a lot of redundant calls to list().

Here is a shorter version:

"""scrambles a word, but creates plausable gibberish"""
import random
def shuffled(s):
""" scrambles word"""
l = list(s)
random.shuffle(l)
return ''''.join(l)
def newZip(a1,a2):
""" reassemble """
l1=len(a1)
l2=len(a2)

longest, shortest = [[a1,a2], [a2,a1]][l1<l2]
diff = max(l1,l2)-min(l1,l2)

seq = len(longest)
ret = ""
for j in range(seq):
if len(longest)>0:
ret += longest.pop()
if len(shortest)>0:
ret += shortest.pop()
return ret

def reinterpolate(word):
""" main function """
wlist = shuffled(word)
vlist = ''aeiouy'' # ok, y isn''t really a vowel, but...
vees = [ x for x in wlist if x in vlist ]
cons = [ x for x in wlist if x not in vlist ]
return newZip(vees,cons)

word = "encyclopedia"
print reinterpolate(word)

Kent
#--------------------------begin code------------------
"""scrambles a word, but creates plausable gibberish"""
import random
def shuffled(s):
""" scrambles word"""
l = list(s)
random.shuffle(l)
return ''''.join(l)

def contains(alist,b):
"""...is letter b in list a..."""
ret = []
for all in alist:
#print all
if all==b:
return 1
return 0

def newZip(a1,a2):
""" reassemble """
l1=len(a1)
l2=len(a2)

longest = [a1,a2][l1<l2]
shortest = [a1,a2][longest == a1]
diff = max(l1,l2)-min(l1,l2)
#print longest
seq = len(longest)
ret = ""
for j in range(seq):
if len(longest)>0:
ret = ret + longest.pop()
if len(shortest)>0:
ret = ret + shortest.pop()
return ret

def reinterpolate(word):
""" main function """
wlist = shuffled(list(word))
vlist = list(''aeiouy'') # ok, y isn''t really a vowel, but...
vees = filter(lambda x: contains(vlist,x),wlist)
cons = filter(lambda x: not(contains(vlist,x)),wlist)
a=list(vees)
b=list(cons)
return newZip(a,b)

word = "encyclopedia"
print reinterpolate(word)

#-------------------------------end code---------------------------



Grooooops写道:
Grooooops wrote:
大家好,

我已经潜伏了一个月的名单,这是我的第一篇文章。我希望这篇文章在这里是合适的,否则,我的道歉。

我对Python有点新鲜,(我正在阅读我能找到的所有教程,我正在尝试学习Python的思维方式以及语法。

我把这些代码拼凑在一起为了好玩,基于之前的帖子
关于随机化一个单词。
这会混淆一个单词,然后拆分元音,然后用辅音内插的元音重新组装它。
(创造合理的声音胡言乱语)

代码对我来说似乎有些笨重。我想知道,这是一种有效的做事方式,还是我要把事情变得更加艰难?

#----------- ---------------开始代码------------------
"""扰乱一个词,但是创造可信的乱码"
导入随机
def shuffled(s):
""" scrambles word"
l = list(s)
random.shuffle(l)
return''''。join(l)

def contains(alist,b):
""" ... is list b in list a ...""
ret = []
for all in alist:
#print all
如果全部== b:
返回1
返回0


" contains(alist,b )"可以更容易地写成b in alist

def newZip(a1,a2):
"""重新组装"
l1 = len(a1)
l2 = len(a2)

最长= [a1,a2] [l1< l2] 最短= [a1,a2] [最长== a1]


最长,最短=已排序([a1,a2],关键= len)

diff = max(l1,l2)-min(l1,l2)
#print longest
seq = len(最长)
ret =""


不要用+构建字符串。创建一个字符串列表,最后使用

str.join。

表示范围内的j(seq):
如果len(最长)> 0:


len()> 0是不必要的,只需使用:


如果最长:

ret = ret + longest.pop()
如果len(最短)> 0:


如果最短:

ret = ret + shortest.pop()
返回


如果我理解你的话,你想做拉链上常见的事情,但

guaraneeing第一个元素总是来自更长的列表,并且

不投掷离开的元素。注意,map(None,...)可能会做你想要的




最长,最短=已排序([a1,a2],key = len,reverse = True)

return''''。join(item

for pair in map(无,最长,最短)

对于项目对

如果项目不是无)


def reinterpolate(word):
"""主要功能""
wlist = shuffled(list(word))
vlist = list(''aeiouy'')#ok,y不是真正的元音,但是.. 。


无需将vlist列入清单;包含应该在字符串上正常工作。

vees = filter(lambda x:contains(vlist,x),wlist)


vees = [c in vlist for c in wlist]

cons = filter(lambda x:not(contains(vlist,x)),wlist)


cons = [c not in vlist for w in wlist]

a = list(vees)
b = list(cons)


以上是不必要的。 vees和cons已经是列表。

返回newZip(a,b)

word =" encyclopedia"
print reinterpolate(word)
Hi All,

I''ve been lurking the list for a month and this is my first post. I am
hoping this post is appropriate here, otherwise, my apologies.

I''m somewhat new to Python, (I''m reading all the tutorials I can find,
and have read through Andre Lessa''s Developers Handbook.)
I am trying to learn the Python way of thinking as well as the syntax.

I popped this bit of code together for fun, based on a previous post
regarding randomizing a word.
This shuffles a word, then splits out the vowels and then reassembles
it with the vowels interpolated between consonants.
(To create plausible sounding gibberish)

The code just seems kind of bulky to me. I am wondering, is this an
efficient way to do things, or am I making things harder than
necessary?

#--------------------------begin code------------------
"""scrambles a word, but creates plausable gibberish"""
import random
def shuffled(s):
""" scrambles word"""
l = list(s)
random.shuffle(l)
return ''''.join(l)

def contains(alist,b):
"""...is letter b in list a..."""
ret = []
for all in alist:
#print all
if all==b:
return 1
return 0
"contains(alist, b)" can be more easily written as "b in alist"


def newZip(a1,a2):
""" reassemble """
l1=len(a1)
l2=len(a2)

longest = [a1,a2][l1<l2]
shortest = [a1,a2][longest == a1]
longest, shortest = sorted([a1, a2], key=len)
diff = max(l1,l2)-min(l1,l2)
#print longest
seq = len(longest)
ret = ""
Don''t build up strings using +. Create a list of strings, and use
str.join at the end.
for j in range(seq):
if len(longest)>0:
The len() > 0 is unnecessary, simply use:

if longest:
ret = ret + longest.pop()
if len(shortest)>0:
if shortest:
ret = ret + shortest.pop()
return ret
If I understand you right, you want to do the usual thing zip does, but
guaraneeing that the first element is always from the longer list, and
not throwing away elements. Note that map(None, ...) probably does what
you want:

longest, shortest = sorted([a1, a2], key=len, reverse=True)
return ''''.join(item
for pair in map(None, longest, shortest)
for item in pair
if item is not None)

def reinterpolate(word):
""" main function """
wlist = shuffled(list(word))
vlist = list(''aeiouy'') # ok, y isn''t really a vowel, but...
no need to make vlist a list; contains should work fine on strings.
vees = filter(lambda x: contains(vlist,x),wlist)
vees = [c in vlist for c in wlist]
cons = filter(lambda x: not(contains(vlist,x)),wlist)
cons = [c not in vlist for c in wlist]
a=list(vees)
b=list(cons)
The above are unnecessary. vees and cons are already lists.
return newZip(a,b)

word = "encyclopedia"
print reinterpolate(word)




所以我认为整体而言,我对你的代码的重写看起来像

(未经测试):


def reinterpolate(word)

wlist = list(word)

random.shuffle(wlist)

vlist =''aeiouy''

vees = [如果c在vlist中,则为c中的c]

cons = [如果c不在vlist中,则为c中的c]

最长, shortest = sorted([vees,cons],key = len)

return''''。join(item

for pair in map(None,long,short)

for item中的物品

if item is not None)



So I think overall, my rewrite of your code would look something like
(untested):

def reinterpolate(word)
wlist = list(word)
random.shuffle(wlist)
vlist = ''aeiouy''
vees = [c for c in wlist if c in vlist]
cons = [c for c in wlist if c not in vlist]
longest, shortest = sorted([vees, cons], key=len)
return ''''.join(item
for pair in map(None, longest, shortest)
for item in pair
if item is not None)


Grooooops写道:
Grooooops wrote:

我一直潜伏在名单上一个月,这是我的第一篇文章。我希望这篇文章在这里是合适的,否则,我的道歉。

我对Python有点新鲜,(我正在阅读我能找到的所有教程,我正在尝试学习Python的思维方式以及语法。

我把这些代码拼凑在一起为了好玩,基于之前的帖子
关于随机化一个单词。
这会混淆一个单词,然后拆分元音,然后用辅音内插的元音重新组装它。
(创造合理的声音胡言乱语)


为了缩短它,我的版本是:


随机导入

def reinterpolate2(word,vocals =''aeiouy''):

wlist = list(word)

random.shuffle(wlist)

vees = [c for w in [:: - 1]如果c在人声中]

cons = [c for c in wlist [:: - 1]如果c不在人声中]

short,long = sorted((cons,vees),key = len)

返回''''。join(long [i] + short [i] for i in range(len(short)))+''''。join(long [len(short):])


代码对我来说似乎有点笨重。我想知道,这是一种有效的做事方式,还是我把事情变得更加艰难?


您对代码的一些评论:

#----------------------- ---开始代码------------------
"""扰乱一个词,但创造了可信的乱码"
随机导入
def shuffled(s):
""" scrambles word"
l = list(s)
random.shuffle(l)
return''''。join(l)


你可以单独定义这个功能,但是不需要。

def包含(alist,b):
""" ...是列表中的字母b a ...""
ret = []
所有人都在alist:
#print all
如果全部== b:
返回1
返回0


完全没必要 - 使用b in alist :)

def newZip(a1,a2):
"""重新组装"
l1 = len(a1)
l2 = len(a2)

最长= [a1,a2] [l1< l2] shortest = [a1,a2] [longest == a1]
diff = max(l1,l2)-min(l1,l2)


diff = abs(l2 -l1)会更短。

#print longest
seq = len(最长)
ret =""
对于范围内的j(seq):
如果len(最长)> 0:
ret = ret + longest.pop()


ret + = longest.pop()浮现在脑海中。

如果len(最短)> 0:
ret = ret + shortest.pop()
返回ret

def reinterpolate(word):
""" main函数""
wlist = shuffled(list(word))


shuffled()会自己创建一个列表,所以list()在这里是多余的。

vlist = list(''aeiouy'')#ok,y不是真正的元音,但是......
vees = filter(lambda x:contains(vlist,x ),wlist)


在这里使用列表理解,如上所述。

cons = filter(lambda x:not(contains(vlist,x)), wlist)
a = list(vees)
b = list(cons)
返回newZip(a,b)

word =" encyclopedia"
print reinterpolate(word)

#-------------------------------结束代码 - -------------------------
Hi All,

I''ve been lurking the list for a month and this is my first post. I am
hoping this post is appropriate here, otherwise, my apologies.

I''m somewhat new to Python, (I''m reading all the tutorials I can find,
and have read through Andre Lessa''s Developers Handbook.)
I am trying to learn the Python way of thinking as well as the syntax.

I popped this bit of code together for fun, based on a previous post
regarding randomizing a word.
This shuffles a word, then splits out the vowels and then reassembles
it with the vowels interpolated between consonants.
(To create plausible sounding gibberish)
To make it short, my version is:

import random
def reinterpolate2(word, vocals=''aeiouy''):
wlist = list(word)
random.shuffle(wlist)
vees = [c for c in wlist[::-1] if c in vocals]
cons = [c for c in wlist[::-1] if c not in vocals]
short, long = sorted((cons, vees), key=len)
return ''''.join(long[i] + short[i] for i in range(len(short))) + ''''.join(long[len(short):])

The code just seems kind of bulky to me. I am wondering, is this an
efficient way to do things, or am I making things harder than
necessary?
Some comments on your code:
#--------------------------begin code------------------
"""scrambles a word, but creates plausable gibberish"""
import random
def shuffled(s):
""" scrambles word"""
l = list(s)
random.shuffle(l)
return ''''.join(l)
You can define this function separately, but needn''t.
def contains(alist,b):
"""...is letter b in list a..."""
ret = []
for all in alist:
#print all
if all==b:
return 1
return 0
That is entirely unnecessary - use "b in alist" :)
def newZip(a1,a2):
""" reassemble """
l1=len(a1)
l2=len(a2)

longest = [a1,a2][l1<l2]
shortest = [a1,a2][longest == a1]
diff = max(l1,l2)-min(l1,l2)
diff = abs(l2-l1) would be shorter.
#print longest
seq = len(longest)
ret = ""
for j in range(seq):
if len(longest)>0:
ret = ret + longest.pop()
ret += longest.pop() comes to mind.
if len(shortest)>0:
ret = ret + shortest.pop()
return ret

def reinterpolate(word):
""" main function """
wlist = shuffled(list(word))
shuffled() will make a list itself, so list() is superfluous here.
vlist = list(''aeiouy'') # ok, y isn''t really a vowel, but...
vees = filter(lambda x: contains(vlist,x),wlist)
Use a list comprehension here, like above.
cons = filter(lambda x: not(contains(vlist,x)),wlist)
a=list(vees)
b=list(cons)
return newZip(a,b)

word = "encyclopedia"
print reinterpolate(word)

#-------------------------------end code---------------------------



这篇关于蟒蛇的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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