Dumb Stupid Question About List and String

Alban Nona python.koda at gmail.com
Tue Aug 31 23:57:45 CEST 2010


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 ?


2010/8/31 Alban Nona <python.koda at gmail.com>

> Well, I have a lot to learn :)
>
> Thank you very much both of you ! it seems to work now :p
>
> 2010/8/31 MRAB <python at mrabarnett.plus.com>
>
>> On 31/08/2010 20:20, Alban Nona wrote:
>>
>>  Ok, here a solution:
>>>
>>> myFirstList = ["FN067_098_MEN", "FN067_098_JIN", "FN067_098_BG"]
>>>
>>> mySecondList =
>>>
>>> ["FN067_098_MEN_Hair_PUZ_v001.0001.exr","FN067_098_JIN_Hair_SPC_v001.0001.exr","FN067_098_MEN_Jin_MVE_v001.0001.exr","FR043_010_GEN_NRM_v001.0001.exr"]
>>>
>>> for n in myFirstList:
>>>      var = str(n)
>>>
>>
>> Why str(n)?
>>
>> Also, it would be clearer if you used different variables for the
>> different loops.
>>
>>
>>       for n in mySecondList:
>>>          if var in n:
>>>               mySecondList.remove(n)
>>>
>>
>> You shouldn't change the length of a list over which you're iterating.
>> Python will step along the list one entry at a time and won't notice when
>> you remove an entry, so the next one will be skipped. For example:
>>
>> >>> letters = ["a", "b", "c", "d", "e"]
>> >>> for i in letters:
>> ...     if i == "b" or i == "c":
>> ...         letters.remove(i)
>> ...
>> >>> print letters
>> ['a', 'c', 'd', 'e']
>>
>> It removed "b" and then moved on to the next entry, which is "d"
>> because "b" has been removed and all the following entries have moved
>> down one place. It never sees "c".
>>
>>
>>> print mySecondList
>>>
>>>  [snip]
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100831/bc66142a/attachment.html>


More information about the Python-list mailing list