【发布时间】:2016-08-05 17:40:19
【问题描述】:
我正在尝试运行一个计数器平衡程序。我无法理解 Python 正在做什么来获得我收到的输出。我有 16 张地图的 8 种变体。变体由timingConditions * distractionConditions 定义。初始变量定义有 4 个关键部分:
-
inst是一个包含所有 8 个变体 * 16 个实验图的列表 按变体排序(这些地图实际上就像电子游戏地图)。这留下了len(inst) = 128。 -
inst在conds中被细分为 8 个子列表。这些中的每一个 sublists 表示特定变体的映射 1-16。每个索引 在子列表中表示一个映射/变体组合。调用conds中每个子列表的索引会清楚地显示这一点。 这 16 个映射分为 8 个列表,每个列表包含两个映射,在变量
MapGroups中定义。这 8 个列表用于下面的矩阵。-
counterBalanceMatrix表示映射到条件分配的八个独特平衡排序。range(1,9)中的每个主题都分配到这些行之一。行中的数字代表地图组。列(即索引排序)表示将变量分配给映射组。例如counterBalanceMatrix[0][0]返回1,第一个索引对应第一个变量列SSTrue的赋值;第二个索引对应于MapGroups[0](将返回'0','15')。因此,所需的输出将是映射 0 和 15(或没有基于零的排序的 1 和 16)分配为 SS-True。你可以这样描绘:####+--------+--------+--------+--------+---------+---------+---------+---------+ ####| SSTrue | SLTrue | LSTrue | LLTrue | SSFalse | SLFalse | LSFalse | LLFalse | ####+--------+--------+--------+--------+---------+---------+---------+---------+ ####| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ####| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 | ####| 3 | 4 | 5 | 6 | 7 | 8 | 1 | 2 | ####| 4 | 5 | 6 | 7 | 8 | 1 | 2 | 3 | ####| 5 | 6 | 7 | 8 | 1 | 2 | 3 | 4 | ####| 6 | 7 | 8 | 1 | 2 | 3 | 4 | 5 | ####| 7 | 8 | 1 | 2 | 3 | 4 | 5 | 6 | ####| 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | ####+--------+--------+--------+--------+---------+---------+---------+----- ----+
subject in range(1,9): 下方代码的预期输出,每个 MapGroup 将有一个实例,每个变体有两个观察值(例如 SS-TRUE、LL-False 等)。在所有主题中,所有MapGroups 和变体都会有相同的观察结果。 这部分代码按预期工作。
import re
timingConditions = ["SS","SL","LS","LL"]
distractionConditions = ["True","False"]
maps = [i for i in range(1,17)]
#This loop sets up the 128 (16*8) variants. Inst for instances of scenario.
inst = []
for d in distractionConditions:
for t in timingConditions:
for map in maps:
inst.append(str(str(map)+"-"+t+"-"+d))
conds = [inst[0:16],inst[16:32],inst[32:48],inst[48:64],inst[64:80],inst[80:96],inst[96:112],inst[112:128]]
MapGroups= [[0,8],
[1,9],
[2,10],
[3,11],
[4,12],
[5,13],
[6,14],
[7,15]]
counterBalanceMatrix= [[1,2,3,4,5,6,7,8],
[2,3,4,5,6,7,8,1],
[3,4,5,6,7,8,1,2],
[4,5,6,7,8,1,2,3],
[5,6,7,8,1,2,3,4],
[6,7,8,1,2,3,4,5],
[7,8,1,2,3,4,5,6],
[8,1,2,3,4,5,6,7]]
for subject in range(1,9):
cRow = counterBalanceMatrix[(subject-1)%8]#Use the modulus operator to find out which row to use in counterbalancing for this subject.
scenArray = []
for group in range(0,8):#This loops across map groups and look up their assigned interruption condition
scenArray.extend([conds[cRow[group]-1][i] for i in MapGroups[group]])
print "****","Participant",subject,"Correct Day 1****"
print scenArray
print "\n\n"
这就是问题所在:
我想重复这个过程,但镜像。也就是说,无论每个list in MapGroups 最初分配的变量是什么,我都希望它被反转(例如,如果您收到MapGroups[0] 为True,那么我希望它们为False。MapGroups[0] 被分配了SS,现在它必须是LL .
我最初的解决方案是反转counterBalanceMatrix 并应用相同的循环。但是这不起作用:
counterBalanceMatrixReverse= []
for list in counterBalanceMatrix:
counterBalanceMatrixReverse.append(list[::-1])
###And then run the exact same loop over.
for subject in range(1,9):
cRow = counterBalanceMatrixReverse[(subject-1)%8]
#
scenArray = []
for group in range(0,8):
scenArray.extend([conds[cRow[group]-1][i] for i in MapGroups[group]])
print "****","Participant",subject,"Broken Reversed 2****"
print scenArray
print "\n\n"
输出不正确,例如:
>Participant 4
>'2-SL-True', '10-SL-True'
Participant 4 Reversed
>'2-SS-False', '10-SS-False'
Exptected Reversed Ouput:
>'2-LS-False', '10-LS-False'
但是,简单地反转 cond 数组确实解决了我的问题
condsreverse = []
condsreverse.extend(conds[::-1])
for subject in range(1,9):
cRow = counterBalanceMatrix[(subject-1)%8]#Note I use same matrix
scenArray = []
for group in range(0,8):
scenArray.extend([condsreverse[cRow[group]-1][i] for i in MapGroups[group]])
print "****","Subject",subject,"Correct Reverse****"
print scenArray
print "\n\n"
我已经坐了三天了,我无法弄清楚为什么最初的反转解决方案没有产生所需的输出。
【问题讨论】:
-
嗯,但是为什么反转行不会同时反转
[condsreverse[cRow[group]-1][i] for i in MapGroups[group]]中的索引分配 -
Hrm,我必须说我对代码中的各种矩阵以及在哪里反转的内容感到非常困惑。
-
很抱歉给您带来了困惑。我有两个可以单独对待的反转实例。正如您所提到的,在一个中,我颠倒了列,而在另一个中,我颠倒了行。第二个 sn-p 我通过反转列表来反转矩阵中的行。第三个 sn-p 我反转列。但是,如果您想象反转行,那么这应该与列反转具有相同的效果。然而,我得到两种不同的输出,这取决于我做哪一种。很明显,我的行反转程序是有缺陷的,但我已经调试过了,无法找出原因。
标签: python python-2.7 nested-lists experimental-design