PYTHON :使用 2 个 2 列列表制作 4 列列表

问题描述

如果问题已经回答,我真的很抱歉,我还没有找到正确的主题...... 我正在与 Pavlovia 和 Pyschopy 合作一个项目。

我希望我的参与者将鼠标光标保持在移动框(目标)内。 我正在记录数据,我想:

  1. 记录鼠标和目标的位置
  2. 计算每一帧中鼠标到目标中心的距离
  3. 计算鼠标开箱的频率

问题在第 1 步和第 2 步之间:

  • 我可以在文件“Mouse.csv”中记录鼠标的位置(x 列和 y 列)
  • 我可以在“Target.csv”文件中记录目标的位置(x 列和 y 列)

但我不能在 1 个文件中包含 4 列的位置(鼠标 x ;鼠标 y ;目标 x ;目标 y)

我的问题非常类似于:

但它无法正常工作

列表看起来像:

  • 鼠标:[['x1','y1'],['x2','y2'],[...]]
  • 目标:[['xA','yA'],['xB','yB'],[...]]

我想得到 [['x1',['xA',[...]]

但是当我可以加入列表时,我只会有类似的东西 [['x1',[...]] :-(

########## FILE 2 ###########
# data to be written row-wise in csv fil
data = MposList
#data  = MposArr   #######_csv.Error: iterable expected,not numpy.float64

# opening the csv file in 'a+' mode
file = open(mousefile,'a+',newline ='') ;

# writing the data into the file
with file:
    # identifying header
    header = ['Mouse at x','Mouse at Y']
    writer = csv.DictWriter(file,fieldnames = header)
    writer.writeheader()
    write = csv.writer(file)
    write.writerows(data)
    
########## FILE 3 ###########
# data to be written row-wise in csv fil
#Tdata = TposArr
Tdata = TposList
del Tdata[0]
del Tdata[0]
# opening the csv file in 'a+' mode
file = open(targetfile,newline ='') ;

# writing the data into the file
with file:
    # identifying header
    Theader = ['target at x','target at y']
    writer = csv.DictWriter(file,fieldnames = Theader)
    writer.writeheader()
    write = csv.writer(file)
    write.writerows(Tdata)
    
########## FILE 4 ###########
# data to be written row-wise in csv fil
#dataF = []
#dataF.append(mouse.getPos()).append(target.pos)
dataF = pd.DataFrame({'mouse x':mouse.getPos[0],'mouse y':mouse.getPos[1],'target x':target.pos[0],'target y':target.pos[1])
#print(dataF)
#data  = MposArr   #######_csv.Error: iterable expected,not numpy.float64

## opening the csv file in 'a+' mode
#file = open("combiner.csv",newline ='') ;

## writing the data into the file
#with file:
#    # identifying header
#    fheader = ['Mouse at x','Mouse at Y','target at x','target at y']
#    writer = csv.DictWriter(file,fieldnames = fheader)
#    writer.writeheader()
#    write = csv.writer(file)
#    write.writerows(dataF)

dataF.to_csv(combiner.csv,index=False)

整个代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This experiment was created using PsychoPy3 Experiment Builder (v2020.2.10),on February 03,2021,at 21:08
If you publish work using this script the most relevant publication is:

    Peirce J,Gray JR,Simpson S,MacAskill M,Höchenberger R,Sogo H,Kastman E,Lindeløv JK. (2019)
        PsychoPy2: Experiments in behavior made easy Behav Res 51: 195.
        https://doi.org/10.3758/s13428-018-01193-y

"""

from __future__ import absolute_import,division

from psychopy import locale_setup
from psychopy import prefs
from psychopy import sound,gui,visual,core,data,event,logging,clock
from psychopy.constants import (NOT_STARTED,STARTED,PLAYING,PAUSED,STOPPED,FINISHED,pressed,RELEASED,FOREVER)

import numpy as np  # whole numpy lib is available,prepend 'np.'
from numpy import (sin,cos,tan,log,log10,pi,average,sqrt,std,deg2rad,rad2deg,linspace,asarray)
from numpy.random import random,randint,normal,shuffle
import os  # handy system and path functions
import sys  # to get file system encoding
import pandas as pd

from psychopy.hardware import keyboard

# Importing library
import csv



# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(_thisDir)

# Store info about the experiment session
psychopyVersion = '2020.2.10'
expName = 'mouseANDtarget'  # from the Builder filename that created this script
expInfo = {'participant': 'sun','session': '01A'}
#dlg = gui.DlgFromDict(dictionary=expInfo,sortKeys=False,title=expName)
#if dlg.OK == False:
#    core.quit()  # user pressed cancel
expInfo['date'] = data.getDatestr()  # add a simple timestamp
expInfo['expName'] = expName
expInfo['psychopyVersion'] = psychopyVersion

## Data file name stem = absolute path + name; later add .psyexp,.csv,.log,etc
##filename = _thisDir + os.sep + u'data/%s_%s_%s' % (expInfo['participant'],expName,expInfo['date'])
filename = _thisDir + os.sep + u'data/%s_%s_%s' % (expInfo['participant'],expInfo['date'])
mousefilename = 'mousePosition_' + expInfo['participant'] + '_' + expInfo['session']+'_' + expInfo['date'];
mousefile = mousefilename + '.csv'
targetfilename ='targetPosition_' + expInfo['participant'] + '_' + expInfo['session'] +'_' + expInfo['date'];
targetfile = targetfilename + '.csv'


# An ExperimentHandler isn't essential but helps with data saving
thisExp = data.ExperimentHandler(name=expName,version='',extraInfo=expInfo,runtimeInfo=None,originPath='C:\\Users\\melis\\Desktop\\untitled.py',savePickle=True,saveWideText=True,dataFileName=filename)
# save a log file for detail verbose info
logFile = logging.LogFile(filename+'.log',level=logging.EXP)
logging.console.setLevel(logging.WARNING)  # this outputs to the screen,not a file

endExpNow = False  # flag for 'escape' or other condition => quit the exp
frametolerance = 0.001  # how close to onset before 'same' frame

# Start Code - component code to be run after the window creation

# Setup the Window
win = visual.Window(
    size=(3840,2160),fullscr=True,screen=0,winType='pyglet',allowGUI=False,allowStencil=False,monitor='testMonitor',color=[0,0],colorSpace='rgb',blendMode='avg',useFBO=True,units='height') ################################################### size : 1024,769  // 3840,2160 & FullScreen=True
# store frame rate of monitor if we can measure it
expInfo['frameRate'] = win.getActualFrameRate()
if expInfo['frameRate'] != None:
    frameDur = 1.0 / round(expInfo['frameRate'])
else:
    frameDur = 1.0 / 60.0  # Could not measure,so guess

# create a default keyboard (e.g. to check for escape)
defaultKeyboard = keyboard.Keyboard()

# Initialize components for Routine "trial"
trialClock = core.Clock()
mouse = event.Mouse(win=win)
x,y = [None,None]
mouse.mouseClock = core.Clock()
MposList=[]
text = visual.TextStim(win=win,name='text',text='default text',font='Arial',pos=(0,0),height=0.1,wrapWidth=None,ori=0,color='white',opacity=1,languageStyle='LTR',depth=-3.0);
currentMousePos = visual.TextStim(win=win,name='currentMousePos',0.4),height=0.05,color='black',depth=-4.0);
TposList=[[],[]]
target = visual.Rect(
    win=win,name='target',units='pix',width=(800,800)[0],height=(800,800)[1],linewidth=1,lineColor=[1,1,1],lineColorSpace='rgb',fillColor=[1,fillColorSpace='rgb',depth=0.0,interpolate=True)
currentTargetPos = visual.TextStim(win=win,name='currentTargetPos',0.6),depth=-4.0);
# Create some handy timers
globalClock = core.Clock()  # to track the time since experiment started
routineTimer = core.CountdownTimer()  # to track time remaining of each (non-slip) routine

# ------Prepare to start Routine "trial"-------
continueRoutine = True
routineTimer.add(120.000000)  ############################### default 10
# update component parameters for each repeat
# setup some python lists for storing info about the mouse
gotValidClick = False  # until a click is received
sameCount = 0
sameThresh = 300
sameWarning = 200
thisCol = "green"
# keep track of which components have finished
trialComponents = [mouse,text,currentMousePos,target,currentTargetPos]
for thisComponent in trialComponents:
    thisComponent.tStart = None
    thisComponent.tStop = None
    thisComponent.tStartRefresh = None
    thisComponent.tStopRefresh = None
    if hasattr(thisComponent,'status'):
        thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timetoFirstFrame = win.getFutureFlipTime(clock="Now")
trialClock.reset(-_timetoFirstFrame)  # t0 is time of first possible flip
frameN = -1

# -------Run Routine "trial"-------
while continueRoutine and routineTimer.getTime() > 0:
    # get current time
    t = trialClock.getTime()
    tThisFlip = win.getFutureFlipTime(clock=trialClock)
    tThisFlipGlobal = win.getFutureFlipTime(clock=None)
    frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
    # update/draw components on each frame
    # *mouse* updates
    if mouse.status == NOT_STARTED and t >= 0.0-frametolerance:
        # keep track of start time/frame for later
        mouse.frameNStart = frameN  # exact frame index
        mouse.tStart = t  # local t and not account for scr refresh
        mouse.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(mouse,'tStartRefresh')  # time at next scr refresh
        mouse.status = STARTED
        mouse.mouseClock.reset()
        prevButtonState = mouse.getpressed()  # if button is down already this ISN'T a new click
    if mouse.status == STARTED:
        # is it time to stop? (based on global clock,using actual start)
        if tThisFlipGlobal > mouse.tStartRefresh + 10-frametolerance:
            # keep track of stop time/frame for later
            mouse.tStop = t  # not accounting for scr refresh
            mouse.frameNStop = frameN  # exact frame index
            win.timeOnFlip(mouse,'tStopRefresh')  # time at next scr refresh
            mouse.status = FINISHED
    if mouse.status == STARTED:  # only update if started and not finished!
        buttons = mouse.getpressed()
        if buttons != prevButtonState:  # button state changed?
            prevButtonState = buttons
            if sum(buttons) > 0:  # state changed to a new click
                # abort routine on response
                continueRoutine = False
    MposList.append(mouse.getPos())
    print("mmmmoooouuuuuussssee ",mouse.getPos())
    Mpos = np.array(MposList)
    MposArr = Mpos.ravel()
    print("MOUSE type ",type(MposArr)) ############ MposArr
    print("MOUSE dimension ",MposArr.ndim) ######### MposArr


    if len(MposList) > 2:
        if MposList[-1][0] == MposList[-2][0]:
            if MposList[-1][1] == MposList[-2][1]:
                #print('same')
                sameCount += 1
        else:
            thisCol = "green"
            sameCount = 0

    if sameCount >= sameWarning:
        thisCol = "red"
    if sameCount >= sameThresh:
        continueRoutine = False
        print(" mouse not moved for " + str(sameCount)+ " frames. Ending experiment")

    # *text* updates
    if text.status == NOT_STARTED and tThisFlip >= 0.0-frametolerance:
        # keep track of start time/frame for later
        text.frameNStart = frameN  # exact frame index
        text.tStart = t  # local t and not account for scr refresh
        text.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(text,'tStartRefresh')  # time at next scr refresh
        text.setAutoDraw(True)
    if text.status == STARTED:
        # is it time to stop? (based on global clock,using actual start)
        if tThisFlipGlobal > text.tStartRefresh + 10-frametolerance:
            # keep track of stop time/frame for later
            text.tStop = t  # not accounting for scr refresh
            text.frameNStop = frameN  # exact frame index
            win.timeOnFlip(text,'tStopRefresh')  # time at next scr refresh
            text.setAutoDraw(False)
    if text.status == STARTED:  # only update if drawing
        text.setColor(thisCol,colorSpace='rgb')
        text.setText("Mouse not moved for: " + str(sameCount) + " frames")

    # *currentMousePos* updates
    if currentMousePos.status == NOT_STARTED and tThisFlip >= 0.0-frametolerance:
        # keep track of start time/frame for later
        currentMousePos.frameNStart = frameN  # exact frame index
        currentMousePos.tStart = t  # local t and not account for scr refresh
        currentMousePos.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(currentMousePos,'tStartRefresh')  # time at next scr refresh
        currentMousePos.setAutoDraw(True)
    if currentMousePos.status == STARTED:
        # is it time to stop? (based on global clock,using actual start)
        if tThisFlipGlobal > currentMousePos.tStartRefresh + 10-frametolerance:
            # keep track of stop time/frame for later
            currentMousePos.tStop = t  # not accounting for scr refresh
            currentMousePos.frameNStop = frameN  # exact frame index
            win.timeOnFlip(currentMousePos,'tStopRefresh')  # time at next scr refresh
            currentMousePos.setAutoDraw(False)
    if currentMousePos.status == STARTED:  # only update if drawing
        currentMousePos.setText("Current pos: " +str(mouse.getPos()))

    # *target* updates
    if target.status == NOT_STARTED and tThisFlip >= 0.0-frametolerance:
        # keep track of start time/frame for later
        target.frameNStart = frameN  # exact frame index
        target.tStart = t  # local t and not account for scr refresh
        target.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(target,'tStartRefresh')  # time at next scr refresh
        target.setAutoDraw(True)
    if target.status == STARTED:
        # is it time to stop? (based on global clock,using actual start)
        if tThisFlipGlobal > target.tStartRefresh + 10-frametolerance:
            # keep track of stop time/frame for later
            target.tStop = t  # not accounting for scr refresh
            target.frameNStop = frameN  # exact frame index
            win.timeOnFlip(target,'tStopRefresh')  # time at next scr refresh
            target.setAutoDraw(False)
    #TposList.append([target.pos[0],target.pos[1]])
    TposList.append(target.pos)
    #del TposList[0]
    print('taaaaggggeeett',target.pos)
    #print("target dimension ",TposList.ndim)
    TposArr = np.array(TposList)
    print("TARGET type ",type(TposArr))
    print("TARGET dimension ",TposArr.ndim)
    #print('0=',TposList[0],'et 1=',TposList[1],'et 2=',TposList[2])
    
    
    # *currentTargetPos* updates
    if currentTargetPos.status == NOT_STARTED and tThisFlip >= 0.0-frametolerance:
        # keep track of start time/frame for later
        currentTargetPos.frameNStart = frameN  # exact frame index
        currentTargetPos.tStart = t  # local t and not account for scr refresh
        currentTargetPos.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(currentTargetPos,'tStartRefresh')  # time at next scr refresh
        currentTargetPos.setAutoDraw(True)
    if currentTargetPos.status == STARTED:
        # is it time to stop? (based on global clock,using actual start)
        if tThisFlipGlobal > currentTargetPos.tStartRefresh + 10-frametolerance:
            # keep track of stop time/frame for later
            currentTargetPos.tStop = t  # not accounting for scr refresh
            currentTargetPos.frameNStop = frameN  # exact frame index
            win.timeOnFlip(currentTargetPos,'tStopRefresh')  # time at next scr refresh
            currentTargetPos.setAutoDraw(False)
    if currentTargetPos.status == STARTED:  # only update if drawing
        Tpx = str(target.pos[0])
        Tpy = str(target.pos[1])
        targetPosition = 'Current target pos: x ='+Tpx+' et y = '+Tpy
        currentTargetPos.setText(targetPosition)


################### count(mouse x) pour compter le nombre de ligne (pour la moyenne)


    # check for quit (typically the Esc key)
    if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
        core.quit()

    # check if all components have finished
    if not continueRoutine:  # a component has requested a forced-end of Routine
        break
    continueRoutine = False  # will revert to True if at least one component still running
    for thisComponent in trialComponents:
        if hasattr(thisComponent,"status") and thisComponent.status != FINISHED:
            continueRoutine = True
            break  # at least one component has not yet finished

    # refresh the screen
    if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
        win.flip()

# -------Ending Routine "trial"-------
for thisComponent in trialComponents:
    if hasattr(thisComponent,"setAutoDraw"):
        thisComponent.setAutoDraw(False)
# store data for thisExp (ExperimentHandler)





###############################################################
x,y = mouse.getPos()
buttons = mouse.getpressed()
thisExp.addData('last x',x)
thisExp.addData('last y',y)
#thisExp.addData('mouse.leftButton',buttons[0])
#thisExp.addData('mouse.midButton',buttons[1])
#thisExp.addData('mouse.rightButton',buttons[2])
#thisExp.addData('mouse.started',mouse.tStart)
#thisExp.addData('mouse.stopped',mouse.tStop)
thisExp.addData('x',MposList)
thisExp.nextEntry()
print('MOUSE LIST',MposList)
print('MOUSE ARRAY',MposArr)
#print('last mouse position',MposList[-1][0],MposList[-1][1])
print('last mouse position',MposList [-1][0],MposList[-1][1])
print('TARGET LIST',TposList)
print('TARGET ARRAY',TposArr)
#thisExp.addData('text.started',text.tStartRefresh)
#thisExp.addData('text.stopped',text.tStopRefresh)
#thisExp.addData('currentMousePos.started',currentMousePos.tStartRefresh)
#thisExp.addData('currentMousePos.stopped',currentMousePos.tStopRefresh)
thisExp.addData('target.started',target.tStartRefresh)
thisExp.addData('target.stopped',target.tStopRefresh)
###################################################################################################


########## FILE 2 ###########
# data to be written row-wise in csv fil
data = MposList
#data  = MposArr   #######_csv.Error: iterable expected,index=False)

# Flip one final time so any remaining win.callOnFlip()
# and win.timeOnFlip() tasks get executed before quitting
win.flip()

# these shouldn't be strictly necessary (should auto-save)
thisExp.saveAsWideText('filename'+'.csv',delim='auto')
thisExp.saveAsPickle(filename)
logging.flush()
# make sure everything is closed down
thisExp.abort()  # or data files will save again on exit
win.close()
core.quit()

我想我不明白二维列表是如何工作的,但我已经阅读了很多课程和教程,但我没有诀窍:(

谢谢

编辑: 这是我的结果: [item for sublist in zip(Mouse,Target) for item in sublist]

Almost done !

所以,我想,我需要 [['x1','y1','xA','y2','xB',[...]]

编辑2: 感谢 Akshay Sengal

result of the good code

解决方法

您可以使用 Get-Process -IncludeUserName | Select-Object -Unique | Where-Object {$_.UserName -notlike 'NT AUTHORITY\SYSTEM' -and $_.UserName -notlike 'NT AUTHORITY\NETWORK SERVICE' -and $_.UserName -notlike 'NT AUTHORITY\LOCAL SERVICE'} | Format-Table -Wrap -AutoSize 执行此操作,然后将其展平以取回项目。您可以按元素压缩 2 个列表(两个列表的相应元素一起添加到元组中。

张贴您可以使用 zip,它可以让您flatten 并将元组分解为相应的项目。

列出这个 -

[item for sublist in list for item in sublist]
Mouse = [['x1','y1'],['x2','y2']]
Target = [['xA','yA'],['xB','yB']]

[item for sublist in zip(Mouse,Target) for item in sublist]

编辑:

根据您更新的问题,您可以这样做 -

[['x1',['xA','y2'],'yB']]
Mouse = [['x1','yB']]

out = [i+j for i,j in zip(Mouse,Target)]
out
,

只为那些想要测试我的代码的人,组合文件使用 i[0],i[1],j[0],j[1] 代替 i+j

#data to be written row-wise in csv file
dataF = [[i[0],j[1],k,abs(i[0]-j[0]),abs(i[1]-j[1])] for i,j,k in zip(MposList,Tdata,mIOt)]

列表的类型/维度不适用于普通代码,我添加了 x 和 y