python/psychopy: for loop to split image names
<p>I have a list of images and I want to split their names at the '=' symbol.</p>

<p>e.g. from:</p>

<pre><code>'set_one-C:\Users\Documents\stim\01=aa.png' 
</code></pre>

<p>to </p>

<pre><code>'aa.png'
</code></pre>

<p>I have tried to create a 'for loop' to run through each item in the list and split the names in turn. Despite no error in the console, the names don't appear to be split.</p>

<p>Here is my code and loop:</p>

<pre><code>imgList1 = glob.glob(os.path.join('C:\Users\Documents\stim','*.png'))

set_one   = [visual.ImageStim(win, img, name='set_one-' + img) for img in imgList1[:8]]
set_two   = [visual.ImageStim(win, img, name='set_two-' + img) for img in imgList1[8:16]]

sets = [set_one, set_two]

a1 = sets[0][0]
a2 = sets[0][1]
a3 = sets[0][2]
a4 = sets[0][3]
a5 = sets[0][4]
a6 = sets[0][5]
a7 = sets[0][6]
a8 = sets[0][7]

list = [a1,a2,a3,a4,a5,a6,a7,a8]

print a1

for item in list:

    item = item.name.split('=')[1]

print a1

&gt;&gt;ImageStim(autoLog=True, color=array([ 1.,  1.,  1.]), colorSpace='rgb', contrast=1.0, depth=0, flipHoriz=False, flipVert=False, image=str(...), interpolate=False, mask=None, maskParams=None, name=str(...), opacity=1.0, ori=0.0, pos=array([ 0.,  0.]), size=array([ 18.36534845,  11.47834278]), texRes=128, units='deg', win=Window(...))
&gt;&gt;ImageStim(autoLog=True, color=array([ 1.,  1.,  1.]), colorSpace='rgb', contrast=1.0, depth=0, flipHoriz=False, flipVert=False, image=str(...), interpolate=False, mask=None, maskParams=None, name=str(...), opacity=1.0, ori=0.0, pos=array([ 0.,  0.]), size=array([ 18.36534845,  11.47834278]), texRes=128, units='deg', win=Window(...))
</code></pre>

<p>I know I could simply split them individually like this:</p>

<pre><code>a1 = a1.name.split('=')[1]
a2 = a2.name.split('=')[1] etc..

print a1

&gt;&gt;aa.png
</code></pre>

<p>But I do need to automate this due to the amount of splitting that I need to do. I'm unsure as to why the for loop does not split the names of each image from the list.</p>

<p>Thanks,
Steve</p>

<pre><code>item = item.name.split('=')[1] 
</code></pre>

<p>This doesn't work.... You want this instead:</p>

<pre><code>new_list = []
for item in list:
    new_list.append(item.name.split('=')[1])

print(new_list)
</code></pre>

<p>Since you aren't changing your <code>list</code> in the for loop. You are only changing the item, but you aren't editing it in the list, if that make sense.</p>

<p>Another way around this is the following:</p>

<pre><code>for i, item in enumerate(list):
    list[i] = item.name.split('=')[1]
</code></pre>

<p>Edit:</p>

<p>Seeing how you want to keep the references to a1,a2,...,a8 for whatever reason... here's why you can't do it with what we have right now. Strings are immutable objects, once you change it you are making a new object, of course you can reassign it back to your variable but that's not the point. If you want to keep reference then you should use a dictionary where the keys are your variable names and the value is well the value. </p>

<pre><code>list_of_var = ['a1','a2','a3'] # and so on
list = [a1,a2,a3,a4,a5,a6,a7,a8]
new_dict = {key: value.name.split("=")[1] for value, key in zip(list,list_of_var)}
#call it like new_dict["a1"]
</code></pre>

<p>But at the end of the day if you are dead set on a1 calls with out dictionary, the only way you can do it, is what you have at the end of your question.</p>

python psychopy