Dumb Stupid Question About List and String

MRAB python at mrabarnett.plus.com
Wed Sep 1 00:26:49 CEST 2010


On 31/08/2010 22:57, Alban Nona wrote:
> Just Another Question on this one, Im trying to create that kind of
> thing in code now:
>
> #GENERE ET INCREMENT LE NOM DES ELEMENTS
>
> val = 0
>
> list = ["0", "1", "2", "3"]
>
> listEl = []
>
> for n in list:
>
>       val = val + 1
>
>       next = "00" +str(val)
>
>       elem = "ELM"+next
>
>       listEl.append(elem)
>
>
>
> #INCREMENT LE NOM DES ELEMENTS AVEC LE NOM DES PASSES
>
> listPass = ["DIF","SPC", "RFL", "SSS", "REFR", "ALB", "AMB", "NRM",
> "MVE", "DPF", "SDW", "MAT", "WPP"]
>
> listElem = []
>
> for first in listEl:
>
>       for second in listPass:
>
>           listElem.append(first+"_"+second)
>
>
> print listElem
>
> print listEl
>
>
> What I would like to do is:
>
> if 'ELM001' Contained in one of the entries of listElem, create a new
> list with only 'ELM001' Elements (eg: newList=['ELM001_DIF',
> 'ELM001_SPC', 'ELM001_RFL', 'ELM001_SSS', 'ELM001_REFR', 'ELM001_ALB',
> 'ELM001_AMB', 'ELM001_NRM', 'ELM001_MVE', 'ELM001_DPF', 'ELM001_SDW',
> 'ELM001_MAT', 'ELM001_WPP']
>
> Damn Im sooooooooo lost with this tables and loop, Im always trying to
> understand in which way I should do it.... :/
> Any Ideas please ?
>
How about:

     newList = []
     for elem in listElem:
         if 'ELM001' in elem:
             newList.append(elem)



More information about the Python-list mailing list