silly idea - interesting problem
Magnus Lie Hetland
mlh at idi.ntnu.no
Sun Oct 21 00:23:22 EDT 2001
"scott" <smarsh at hotmail.com> wrote in message
news:3BD248BA.2163BBDB at hotmail.com...
>
> Stefan Antoni wrote:
> >
> > i got a silly idea about a small script which takes to strings (lets say
> > "test" and "dust") and mixes them to "tdeusstt".
> > i tried to code it because i thought it would be easy, but now i cannot
> > find out _how_ to do it ;)
> >
> > i tried to "list()" the string and mix the items. but ...
> >
> > Anybody has a hint or even a solution?
> >
>
> Here's a back to basics solution:
>
> firstWord = 'test'
> secondWord = 'dust'
> i = 0
> while i < len(firstWord):
> print firstWord[i], secondWord[i],
> i = i+1
This will give spaces between every word...
How about a less readable version which actually works <wink>:
print ''.join([x+y for (x,y) in zip('test', 'dust')])
> P.S.: Remember when Python used to differentiate itself from line noise
> (sorry, I meant Perl) with a philosophy that went something like
> "There's only one right way to do it."?
Yes... Those were the days <wink>
A version with spacing (like yours) which is a bit more readable (than
my previous one) would be:
for firstLetter, secondLetter in zip(firstWord, secondWord):
print firstLetter, secondLetter,
One without the spaces could be:
result = ''
for a, b in zip('test', 'dust'):
result += a+b
print result
Yeah... I think I like that one. (Although using a list and then doing
a join on that would probably be more efficient...)
----------------------------------------------------------------------
Magnus Lie Hetland
http://hetland.org
More information about the Python-list
mailing list