psychopy.org | Reference | Downloads | Github

Merge lists A and B, shuffle row-wise, then split into shuffled lists A and B

Hi everyone,

I ran into some issues with list creation within the experiment, i.e., merging two lists:

stimlist A = [“1a.jpg”,“2a.jpg”,…,“192a.jpg”]
stimlistB = [“1b.jpg”,“2b.jpg”,…,“192b.jpg”]

into a table with “stimlistA” and “stimlistB” as variable/column names. This table should be shuffled row-wise/pair-wise, resulting in something like this:

Then I would like to split the shuffled table into two (now shuffled) lists:

stimlistA_shuffled = [“2a.jpg”,“192a.jpg”,…,“1a.jpg”]
stimlistB_shuffled = [“2b.jpg”,“192b.jpg”,…,“1b.jpg”]


When running on my local machine, the following Py code does the job:

stimlist = list(zip(stimlistA,stimlistB))
shuffle(stimlist)
shuffledstimlist = stimlist
stimlistA_shuffled, stimlistB_shuffled = map(list, zip(*stimlist))

However, I have not managed to translate this into JS to be used in an online experiment …

What I´ve tried so far:

stimlist = zip(stimlistA, stimlistB);
shuffle(stimlist);

after inserting the following code at the beginning of the experiment:

shuffle = util.shuffle;
Array.prototype.append = [ ].push;

const zip = (…arr) => {
const zipped = [ ];
arr.forEach((element, ind) => {
element.forEach((el, index) => {
if(!zipped[index]){
zipped[index] = [ ];
};
if(!zipped[index][ind]){
zipped[index][ind] = [ ];
}
zipped[index][ind] = el || ‘’;
})
});
return zipped;
};

URL of experiment: MST [PsychoPy]

Any help is greatly appreciated!

Hi @AKZ , for your JavaScript, you could use

// Create new arrays for shuffled lists
[newListA, newListB] = [[], []];
// Create new indexing array for getting stim list values by row
arrayIndex = [...Array(stimlistA.length).keys()];
// shuffle indexing array
shuffle(arrayIndex);

// Fill new lists with stimlist values shuffled by row
for (let i of arrayIndex) {
    newListA.push(stimListA[i]);
    newListB.push(stimListB[i]);
}



Btw, this does seem like something the trial handler can do, unless you have multiple pairs that need to be shuffled

Hi,

thank you so much for your reply!
This helped a lot and I can follow the logic of the code. However, it only seems to shuffle newlistB but not newlistA according to the arrayIndext.
This is the output I found in the console:
grafik

And this is what I inserted as code in the experiment:

var newlistA;
var newlistB;
// create empty table:
[newlistA, newlistB] = [[ ], [ ]];
// create indexing array for shuffling:
array_shufflingIndex = […Array(stimlistA.length).keys()];
console.log(“array_shufflingIndex”, array_shufflingIndex);
//shuffle indexing array:
shuffle(array_shufflingIndex)
console.log(“array_shufflingIndex_shuffled”, array_shufflingIndex);

for (let i of array_shufflingIndex) {
newlistA.push(stimlistA[i]);
newlistB.push(stimlistB[i]);
}

Does anyone have an idea?

I guess it would be difficult to use the trial handler here as I have 6 item sets with 384 items each and several lists need to be passed to further phases of the experiment, where they should be split again etc. … But if there is an easier way - this would be more than welcome!
Thanks again!

Hi @AKZ , you might want to check that your code is not shuffling those lists elsewhere, because this code should not shuffle those lists in different orders.

Thanks!
It turned out that, for some reason, swapping positions of lists A and B in the for loop solved the problem - now lists are shuffled pair-wise.

for (let i of array_shufflingIndex) {
newlistB.push(stimlistB[i]);
newlistA.push(stimlistA[i]);
}

Thanks a lot @dvbridges, your code was of great help!