-1

I have a list of images and I want to split their names at the '=' symbol.

e.g. from:

'set_one-C:\Users\Documents\stim\01=aa.png' 

to

'aa.png'

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.

Here is my code and loop:

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

>>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(...))
>>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(...))

I know I could simply split them individually like this:

a1 = a1.name.split('=')[1]
a2 = a2.name.split('=')[1] etc..

print a1

>>aa.png

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.

Thanks, Steve

Steve
  • 327
  • 2
  • 3
  • 15
  • What is `list` in your for loop? And does the structure item has the attribute `name`? – MooingRawr Oct 03 '16 at 15:37
  • Sorry, it's a list of the images, I have just edited my original post. And yes, each image does have the attribute 'name'. – Steve Oct 03 '16 at 15:40

1 Answers1

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

This doesn't work.... You want this instead:

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

print(new_list)

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

Another way around this is the following:

for i, item in enumerate(list):
    list[i] = item.name.split('=')[1]

Edit:

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.

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"]

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.

MooingRawr
  • 4,901
  • 3
  • 24
  • 31
  • Oh yes, I understand. Thanks for your help! – Steve Oct 03 '16 at 15:50
  • Actually, I also need to be able to refer to each image as 'a1,'a2' etc. just as I did in the original list. Do you know a way of retaining this? – Steve Oct 03 '16 at 15:59
  • http://stackoverflow.com/a/9967237/4099813 see this answer and see if you want me to give you a work around to still retain reference to a1,a2... Most of the time, there's another way around your issue, instead of focusing on the issue of an answer (X Y issue) – MooingRawr Oct 03 '16 at 16:21
  • Welp I'm going to guess you wanted to stay with your variable references see editted answer – MooingRawr Oct 03 '16 at 18:35
  • Could you give me the workaround to retain a1,a2 etc. ? – Steve Oct 05 '16 at 10:48