Andrei:
>Date: Sat, 29 Oct 2005 01:13:45 +0200
>From: Andrei <[EMAIL PROTECTED]>
>Subject: Re: [Tutor] Recursion and List Comprehensions
>To: tutor@python.org
>Message-ID: <[EMAIL PROTECTED]>
>Content-Type: text/plain; charset=ISO-8859-1; format=flowed
<>
l, Barry
>Sent: Friday, October 28, 2005 5:49 PM
>To: 'Alan Gauld'; Carroll, Barry; tutor@python.org
>Subject: RE: [Tutor] Recursion and List Comprehensions
>
>Alan et al:
>
>After reading the topic you recommended I tried rewriting my permute
>function as follows:
&
ator (retList+= ...) causes this error:
>>>>>>>>>>
>>> permute.permute3('tests')
Traceback (most recent call last):
File "", line 1, in ?
File "permute.py", line 50, in permute3
retList+=[word[pos]+item for item in
perm
> Unfortunately, I don't understand how list comprehensions work and how to
> implement them. Can someone point me in the right direction, please.
Compare these two pieces of code
x=[1,2,3,4]
y=[]
for eachnum in x:
y.append(eachnum * 2)
versus
x=[1,2,3,4]
y = [each * 2 for each in x
Carroll, Barry wrote:
> Greetings:
> I'm trying to improve my programming and Python skills. To that end I
> have implemented a word jumble program as a recursive function: given a
> string of arbitrary length, return a list of all permutations of the
> string each character exactly once. In
Carroll, Barry wrote:
>> >>>permuteList=permute2(word[0:pos]+word[pos+1:len(word)])
>> >>># Now, tack the first char onto each word in the list
>> >>># and add it to the output
>> >>>for item in permuteList:
>> >>>retList.append(word[p
>def permute3 (word):
>retList=[]
>if len(word) == 1:
># There is only one possible permutation
>retList.append(word)
>else:
># Return a list of all permutations using all characters
>retlist = [a list comprehension that ca
Greetings:
I'm trying to improve my programming and Python
skills. To that end I have implemented a word jumble program as a
recursive function: given a string of arbitrary length, return a list of
all permutations of the string each character exactly once. In other
words:
pe