Shuffle list and pop to prevent repeating rows

Taking advice and suggestions from Wakefield's Daily Tips - #4 by wakecarter and Wakefield's Daily Tips - #23 by wakecarter, I’ve tried changing my approach, but now I have a different question (but the same problem - repeating rows). [see original post here - Randomization problem: No error message, yet still repeating rows no matter what i do]

Why do you think it is not registering my items in the list as ‘single items’? It seems to be taking the whole list, and when printing, it doesn’t print the selected row (for the execution trial), instead just the whole range. What can I do to fix it? Is this the reason I am having the issue of using these techniques to prevent repeated rows? Similarly, when I used lists and .pop(), it also had an error.

I changed all the selections from - 0:10, 10:20 etc to the below.

all_selections = [
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
    [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40],
    [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50],
    [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
]

And then kept everything the same. This worked sometimes, and yet others I would have a repeat. (I have examples of output if people are interested). For instance, my print statement for this was [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

# Generate file names based on Comb_list
rl = 't' + str(Comb_list[0]) + '.jpeg'
el = 'b' + str(Comb_list[1]) + '.jpeg'
selected_rows = all_selections[Comb_list[1] - 1]

So then I thought perhaps I should try shuffling the list? While the order was randomised (for example, 8, 3, 6, 2, 9, 1, 0, 10, 7, 4, 5]. It did not fix my problem; honestly, it increased the repeats.


shuffle(selected_row)

I then thought okay, let’s try what this suggested.

selected_range = all_selections[Comb_list[1] - 1]

selected_row_range = shuffle(selected_range)

selected_rows = selected_row_range.pop()


Sadly, this did not work at all. It caused Psychopy to crash without any error and then said there was an attribute error with pop being a nontype.

selected_rows = selected_row_range.pop()
AttributeError: ‘NoneType’ object has no attribute ‘pop’

Try

selected_range = all_selections[Comb_list[1] - 1]

shuffle(selected_range)

selected_rows = selected_range.pop()

print('selected_range',selected_range,selected_rows)

It looks like you only want one row.

Actually, try this.

selected_rows = all_selections[Comb_list[1] - 1].pop()

print('selected_range',all_selections[Comb_list[1] - 1],selected_rows)

That will remove the row from all_selections as opposed to just from your temporary variable.

1 Like

Thank you so much for your suggestion. It kinda worked.

It printed selected_range [0, 1, 2, 3, 4, 5, 7, 8, 9] 10. So, it is kinda is separating them. However, then it immediately crashed and had this error message.

trialList=data.importConditions(groupFile, selection=selected_rows),
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\data\utils.py", line 490, in importConditions
    elif len(selection) > 0:
TypeError: object of type 'int' has no len()
################ Experiment ended with exit code 1

Try

selected_rows = [all_selections[Comb_list[1] - 1].pop()]

to make it a list.

BTW, do you mean to have index 10, 20, 30, etc in two lists each?

1 Like

OMG. It didn’t crash! Thank you so much!!!

I had it in my original code (0:10, 10:20, 20:30, etc.) and tried to follow some of your tips (while trying to apply to mine). So, it just stayed that way, but if it this works, then I only need them once! Hopefully this makes sense.

And it totally worked!!! You are a lifesaver! Seriously! Thanks so much. I am going to remove the double index now. :slight_smile: :raised_hands:

1 Like