博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python执行另一个脚本_另一个带约束的Python脚本
阅读量:2519 次
发布时间:2019-05-11

本文共 5185 字,大约阅读时间需要 17 分钟。

python执行另一个脚本

In this post you will find a function I wrote solving a problem of randomization with constrains in Python. For a psychological experiment () I am planning to conduct I needed a list of stimuli names quasi-randomized.  Fifty percent of the items in the list be followed by a stimulus in the same colour and the remaining 50 % would be followed by a stimulus in a different colour.

在本文中,您将找到我编写的函数,用于解决Python中带有约束的随机化问题。 对于心理实验( ),我计划进行操作,我需要一个准随机命名的刺激物名称列表。 列表中百分之五十的项目后面是相同颜色的刺激,其余的50%之后是不同颜色的刺激。

Python脚本 (Python script)

from random import shufflefrom collections import Counterliststim = []colors, firstfigures = ['Black', 'Blue'], ['Rect', 'Triangle', 'X', 'Circle']secondfigures = firstfiguresfor col in colors:    for figure in firstfigures:        for figure2 in secondfigures:            liststim.append(col + '-' + figure + '_' + figure2)def randShift(listostim, ntrials):    '''    Will randomize 50 % of shifting trials    based on that each filename (of the images) starts with Black or Blue-.        listostim is a list of trials    ntrials is an integer indicating number of trials    '''    nEachstim = ntrials/len(liststim) #Number of each stim is going to be even    stimList = listostim     trialTypes = []     stims = []    countOfStim = dict((el,0) for el in stimList)    count = {'ns':0,'s':0}    for i in range(ntrials):        shuffle(stimList)        for idx in range(len(stimList)):            if not stims:                countOfStim[stimList[idx]] +=1                stims.append(stimList[idx])                count['ns'] +=1                trialTypes.append("No-Shifting")            elif stims:                if count['s'] <= ntrials/2:                    if countOfStim[stimList[idx]] <= nEachstim-1: if stimList[idx][:5] != stims[i-1][:5]: count['s'] +=1 countOfStim[stimList[idx]] +=1 stims.append(stimList[idx]) trialTypes.append("Shifting") else: count['ns'] +=1 countOfStim[stimList[idx]] +=1 stims.append(stimList[idx]) trialTypes.append("No-Shifting") elif count['s'] > ntrials/2:                    if countOfStim[stimList[idx]] <= nEachstim-1:                         if stimList[idx][:5] == stims[i-1][:5]:                            count['ns'] +=1                            countOfStim[stimList[idx]] +=1                            stims.append(stimList[idx])                            trialTypes.append("No-Shifting")    #Frequency of the trialtypes    freq = Counter(trialTypes).values()from random import shufflefrom collections import Counterliststim = []colors, firstfigures = ['Black', 'Blue'], ['Rect', 'Triangle', 'X', 'Circle']secondfigures = firstfiguresfor col in colors:    for figure in firstfigures:        for figure2 in secondfigures:            liststim.append(col + '-' + figure + '_' + figure2)def randShift(listostim, ntrials):    '''    Will randomize 50 % of shifting trials    based on that each filename (of the images) starts with Black or Blue-.        listostim is a list of trials    ntrials is an integer indicating number of trials    '''    nEachstim = ntrials/len(liststim) #Number of each stim is going to be even    stimList = listostim     trialTypes = []     stims = []    countOfStim = dict((el,0) for el in stimList)    count = {'ns':0,'s':0}    for i in range(ntrials):        shuffle(stimList)        for idx in range(len(stimList)):            if not stims:                countOfStim[stimList[idx]] +=1                stims.append(stimList[idx])                count['ns'] +=1                trialTypes.append("No-Shifting")            elif stims:                if count['s'] <= ntrials/2:                    if countOfStim[stimList[idx]] <= nEachstim-1: if stimList[idx][:5] != stims[i-1][:5]: count['s'] +=1 countOfStim[stimList[idx]] +=1 stims.append(stimList[idx]) trialTypes.append("Shifting") else: count['ns'] +=1 countOfStim[stimList[idx]] +=1 stims.append(stimList[idx]) trialTypes.append("No-Shifting") elif count['s'] > ntrials/2:                    if countOfStim[stimList[idx]] <= nEachstim-1:                         if stimList[idx][:5] == stims[i-1][:5]:                            count['ns'] +=1                            countOfStim[stimList[idx]] +=1                            stims.append(stimList[idx])                            trialTypes.append("No-Shifting")    #Frequency of the trialtypes    freq = Counter(trialTypes).values()

The last part is the crucial part since the above code will not produce the required list 100 % of the time. The script, therefore, first checks that the above script has generated the output needed. That is, it is 50% items that are of a color and then followed by a different color? An additional check is also made to make sure that there are so many items that were  required (i.e-, number of trials; ntrials). Last code will call the function again until the requirements are fulfilled.

最后一部分是关键部分,因为上面的代码不会在100%的时间内生成所需的列表。 因此,该脚本首先检查上述脚本是否已生成所需的输出。 也就是说,是50%的商品是一种颜色,然后是另一种颜色? 还进行了另一项检查,以确保所需的项目太多(即,试验次数;试验)。 最后的代码将再次调用该函数,直到满足要求为止。

The script is solving my problem and it works pretty quick. However, if there is a more elegant method I would love to see it.

该脚本正在解决我的问题,并且运行很快。 但是,如果有更优雅的方法,我很乐意看到。

翻译自:

python执行另一个脚本

转载地址:http://vgqwd.baihongyu.com/

你可能感兴趣的文章
Ajax中“Sys未定义”错误的解决方法汇总(转)
查看>>
深入理解分布式事务及高并发下分布式事务的解决方案
查看>>
ORACLE对象大小写问题
查看>>
HDU_6033_Add More Zero
查看>>
hdu 1372
查看>>
Word:转换PDF
查看>>
依赖项属性
查看>>
WPF MVVM模式不用Prism
查看>>
css三级菜单效果
查看>>
javascript语法结构
查看>>
Delphi 语句块《LceMeaning》
查看>>
sqlserver数据库的分离与附加
查看>>
2.4:远程服务器关机及重启时的注意事项
查看>>
内存管理之ARC
查看>>
Myeclipse中左边的项目目录没了
查看>>
linux基础知识和常用命令
查看>>
webpack安装
查看>>
白话ASP.NET MVC之三:Controller是如何解析出来的
查看>>
mac中将jmeter源码导入idea
查看>>
bootstrap table 标题列重复
查看>>