1

I have a simple experiment with 8 different trials that I wish to present only once without repeat; participants are asked to 'solve' a numeric sequence where 4 sequences have a solution (S) and 4 are unsolvable (U).

Thus, I have 8 trials:

S1, S2, S3, S4, U1, U2, U3, U4.

I want to present the trials so that they alternate:

S1, U1, S2, U2, S3, U3, S4, U4

However, I want to randomise the order of S and U, whilst maintaining the alternating pattern.

For example, S3, U2, S2, U4, S4, U1, S1, U3

I am using the builder and am new to Python (although I am familiar with coding in Matlab). The only solutions I can come up with is to try and shuffle the order of the trials in the excel file and then combining them so they alternate - this doesn't seems very elegant though.

Is there a simple way to implement this within the builder or by adding a code component?

I have included the input file / excel table that defines the trials. The column 'difficulty' indicates whether they are Solvable or Unsolvable trials.

cheers, Harry

input

2 Answers2

0

Uses prior answer on list conversions and concept of list comprehensions

import random as r 
a = ["s1","s2","s3","s4"]
b = ["u1","u2","u3","u4"]
r.shuffle(a)
r.shuffle(b)
trialList = [tr for pr in zip(a,b) for tr in pr]
Community
  • 1
  • 1
brittAnderson
  • 1,370
  • 9
  • 22
0

You should zip (or itertools.izip) and then flatten the result using itertools.chain.from_iterable. Using zip takes multiple sequences and creates a new sequence where each item contains the corresponding elements from the original sequences. In your case, it will create something like [(S3, U2), (S2, U4), (S4, U1), (S1, U3)]. You can then flatten using the chain function to get the [S3, U2, S2, U4, S4, U1, S1, U3] sequence you want. shuffle randomizes the order of the two sequences in-place, while list converts the iterator that results from chain into a list:

from itertools import chain, izip
from random import shuffle

solvable = [s1, s2, s3, s4]
unsolvable = [u1, u2, u3, u4]

shuffle(solvable)
shuffle(unsolvable)

trials = list(chain.from_iterable(izip(solvable, unsolvable)))
TheBlackCat
  • 8,821
  • 3
  • 21
  • 29