string generator

James T. Dennis jadestar at idiom.com
Tue Jun 18 02:13:55 EDT 2002


Gustavo Cordova <gcordova at hebmex.com> wrote:

>> I like to create a list of file automatical based on series. 
>> For example
>> user input the number of file say 5. So i want to generate the file 
>> according to this range , this mean the generated files could be 
>> filename1
>> filename2
>> filename3
>> filename4
>> filename5
>> I using the for loop like that
>> 
>> for file in range(5):
>>         lis = [file]
>>         string.join('filename',lis[1:])
>> how can it didn't work. I am confusing how to do it.
>> 

> Yeah, but where do you wanna put your results?

> You could also:
> FNs = ["filename%d" % i for i in range(5)]
> or also you could:
> FNs = map(lambda i:"filename%d" % i, range(5))
> or many other variations thereof.

> Good luck!
> -gustavo

 Here's a variation:

>>> string.split(' filename'.join([str(x) for x in range(5)]))[1:]
['filename1', 'filename2', 'filename3', 'filename4']

>>> string.split(' filename'.join([str(x) for x in range(-1,5)]))[1:]
['filename0', 'filename1', 'filename2', 'filename3', 'filename4']

 although the map(lambda ...)) with the % operatator is even more
 clever.




More information about the Python-list mailing list