两种反转lis方法的输出不一致

2024-06-17 13:46:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在试着运行一个平衡程序。我无法理解Python是如何获得我接收到的输出的。我有16张地图的8种变体。变体由timingConditions*distractionConditions定义。初始变量定义有4个关键部分:

  1. inst是一个包含所有8个变体*16个实验图的列表 按变体排序(这些地图实际上就像一个电子游戏地图)。这就剩下len(inst) = 128。你知道吗
  2. instconds中被细分为8个子列表。每一个 子列表表示特定变体的映射1-16。每个索引 在子列表中表示一个映射/变体组合。调用conds中每个子列表的索引将清楚地显示这一点。你知道吗
  3. 这16个映射被分成8个列表,每个列表包含两个映射,在变量MapGroups中定义。这8个列表用于下面的矩阵。

  4. 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"

我已经在这上面坐了三天了,我不明白为什么最初的反向解决方案不能产生期望的输出。你知道吗


Tags: infalse列表forgrouprange变体ss
1条回答
网友
1楼 · 发布于 2024-06-17 13:46:03

我认为解决办法如下,如果只是为了结束问题。你知道吗

我的错误是我假设[conds[cRow[group]-1]是基于cRow[i]的索引分配变量,cRow[i]只是用来调用特定的映射。然而,实际上所做的是根据cRow[i]的值分配变量。因此,没有根据索引将“列”分配给MapGroups。这意味着简单地反转counterBalanceMatrix基本上就是创建counterBalanceMatrix的另一个版本。你知道吗

我唯一能得到相互镜像的条件的方法是反转conds的顺序,因为这是赋值中实际使用的列表;除非我当然想更改scenArray.extend([condsreverse[cRow[group]-1][i] for i in MapGroups[group]])来调用每个项的索引(对于原始项和反转项),然后基于该索引调用map组,如下所示:

for index in range(0,8):
    scenArray.extend([conds[index][i] for i in MapGroups[cRow[index]-1]])

PS:我想把它写成一个问题帮助我认识到自己的错。很抱歉。

相关问题 更多 >