the python way?

Grooooops JOHNWUN at aol.com
Mon Jun 6 14:27:52 EDT 2005


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




More information about the Python-list mailing list