python csv 到嵌套列表,计数器

标签 python bash csv

我正在尝试做一些在 Python (2.7.6) 中可能不那么困难的事情,但我离解决方案还差得很远。这是用于 fMRI 实验(特别是在实验中)。我有一个看起来像这样的 csv:

level,string,key
1,string11,1
1,string12,0
1,string13,1
2,string21,1
2,string22,1
2,string23,0

有 20 个级别 (1:20),csv 大约有 5000 条记录。我想要做的是从特定级别的第二列中显示一个字符串(起始级别是预先确定的)。因此,例如,我们测试了一个主题并确定他们将从第 5 级开始。从第 5 级开始呈现一个随机字符串(例如,“牛是一种动物”)。受试者按下判断真/假的按钮(“关键”栏中的答案 - 1/0)。棘手的一点是,每 4 个连续正确答案的等级增加 1,错误答案等级降低 1。

时间/效率有些重要,所以我想可以提前解析csv,然后实验过程中工作的部分只是访问级别存储的数据。我看过嵌套词典,似乎不对。也许嵌套列表?我有点不知所措。我在 bash 中写了一个工作示例,以防它比我的解释更清楚。

#!/bin/bash

csv="file.csv"
level="5"
ilevel="0"
counter="1"

while [ "$counter" -le 10 ]; do
    stim=$(awk -F',' -v l="$level" '{if($1==l) print $2","$3}' "$csv"|sort -R|head -n1)
    stim1=$(echo "$stim"|cut -d',' -f1)
    stim2=$(echo "$stim"|cut -d',' -f2)
    read -p "$stim1 (t/f): " ANS 
    if [[ "$stim2" == "1" && "$ANS" == "t" || "$stim2" == "0" && "$ANS" == "f" ]]; then #if correct
        ilevel=$(echo "scale=2;$ilevel+.25"|bc) #add .25 to ilevel counter
        if [[ "$ilevel" == "1.00" ]]; then
            if [[ "$level" < "20" ]]; then
                level=$(echo "$level+1"|bc) #increase level
            fi
            ilevel=0
        fi
    else
        if [[ "$level" > "1" ]]; then #if incorrect
            level=$(echo "$level-1"|bc) #decrease level
        fi
        ilevel=0
    fi
    let "counter += 1"
done

bash 脚本仅用于工作示例 - 计数器、打印到屏幕、用户反馈......在现实中处理方式各不相同,并且已经完成。我只需要弄清楚 csv 的排序和操纵级别。提前感谢您的任何建议。

最佳答案

要对字符串进行分组,请使用字典并使用 random.choice 来选择随机字符串:

 from collections import defaultdict
import csv
from random import choice
d = defaultdict(list)


with open("in.csv") as f:
    next(f)
    r = csv.reader(f)
    for row in r:
        d[int(row[0])].append(row[1])


level = 2

ch = choice(d[level])
print(ch)
string21

如果您需要答案/第 2 列:

from collections import defaultdict

d = defaultdict(defaultdict)

with open("in.csv") as f:
    next(f)
    r = csv.reader(f)
    for row in r:
        d[int(row[0])][row[1]] = row[2]

from pprint import pprint as pp
pp(dict(d))
level = 2
ch = choice(list(d[level]))
ans = d[level][ch]
print(ch,ans)

输出:

 # main dict keys are levels, nested dict contain key/value pairing
 # question and answer
{1: defaultdict(None, {'string11': '1', 'string12': '0', 'string13': '1'}),
 2: defaultdict(None, {'string21': '1', 'string23': '0', 'string22': '1'})}

主字典键是级别,嵌套字典包含所有级别字符串,它们是嵌套字典的键,值是第 3 列的答案。

如果您使用的是 gui,您的逻辑将类似于以下内容,但显然使用的是您的 gui 方法:

level = 2
streak = 0
# current level questions
level_choice = list(d[level])

while True:
    # pick random string/question
    ch = choice(level_choice)
    # correct answer
    ans = d[level][ch]
    inp = raw_input("ask questions using ch")
    # if answer  is correct increment streak
    if inp == ans:
        streak += 1
    else:
        # else it is wrong, drop a level and update variables
        level -= 1
        streak = 0
        level_choice = list(d[level])
    # if we have four in a row, up level ad again update variables
    if streak == 4:
        level += 1
        streak = 0
        level_choice = list(d[level])

显然,一定会出现一个点,其中许多错误的答案将意味着级别达到 0,因此可能是打破的时候了,你必须决定这个逻辑。如果您不想重复问题,则必须将它们从字典中删除。

关于python csv 到嵌套列表,计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30719553/

相关文章:

php - fputcsv 写入空白文件

python - 为什么创建的 csv 文件会重复行以及如何将列表创建为列?

python - Django 的平均收视率

linux/tmp 文件夹 + 如何知道文件是否会在重启后或一段时间后被删除

python - 你如何将 bash 别名移植到 ipython > 0.10?

python - Tweepy StreamListener 到 CSV

python - 将数千条记录插入表中的最有效方法是什么(MySQL、Python、Django)

python - numpy 如何计算指数?

bash,用另一个变量替换部分变量

java - 使用FlatFileItemReader读取csv文件,遇到空列时抛出异常