Complicated program (for me). Help please!

Enrico Morelli enrico_morelli at yahoo.com
Thu May 29 10:54:26 EDT 2003


Dear all,

I'm a newbie of python language and my english is very bad. Excuse me. 
I have to write a program to solve the following problem:
I have a string like: CCC[X1]CCC[X2]CCC[X3]CCC[Xn]
For each Xn I have Xn files that contains some values.
The program should substitute all Xn occurences with all file's values
in all combinations. The lenght of each file should be different.
Each Xn has its file.

For example:
strind: CCC[X]CCC[X]CCC[X]
File1: 1,2,3
File2: 4,5,6
File3: 7,8

Output:
CCC[1]CCC[4]CCC[7]
CCC[1]CCC[5]CCC[7]
CCC[1]CCC[6]CCC[7]
CCC[1]CCC[4]CCC[8]
CCC[2]CCC[4]CCC[7]
CCC[3]CCC[4]CCC[7]
.
.
.

I use the following function to do a similar thing with only one list of
values to substitute (suggest me by Alex Martelli):
(in lastr there is the CCC[X] string and in  valori there are the values
to substitute):
def substitute(pezzi, valori):
    if len(pezzi) < 2:
        print pezzi[0]
        return
    formato = '%s%%s%s' % tuple(pezzi[:2])
    altri = pezzi[2:]
    for valore in valori:
        substitute( [formato%valore]+altri, valori)

sostituisci(lastr.split('X'), valori)

I'm stupid, but I don't know how change this function or write a new one
to solve my problem. Can someone help me? I hope that you understand my
bad english.

Thanks
Enrico





More information about the Python-list mailing list