我有一个如下所示的数据集:
Subject | Session | Trial | Choice
--------+---------+-------+-------
1 | 1 | 1 | A
1 | 1 | 2 | B
1 | 1 | 3 | B
1 | 1 | 4 | B
1 | 1 | 5 | B
1 | 1 | 6 | A
2 | 1 | 1 | A
2 | 1 | 2 | A
2 | 1 | 3 | A
我想使用Python脚本生成下表:
Subject | Session | streak_count
--------+---------+-------------
1 | 1 | 3
2 | 1 | 1
其中streak_count
是给定主题在给定会话期间选择条纹总数的计数,而条纹是连续一个特定项目的任意数量的选择(>0
)。
我已尝试在这里使用一些类似问题的建议,但我无法弄清楚如何计算这些实例,而不是测量它们的长度等,这似乎是更常见的查询。
答案 0 :(得分:0)
我认为这就是你所要求的;
import itertools
data = [
[1, 1, 1, 'A'],
[1, 1, 2, 'B'],
[1, 1, 3, 'B'],
[1, 1, 4, 'B'],
[1, 1, 5, 'B'],
[1, 1, 6, 'A'],
[2, 1, 1, 'A'],
[2, 1, 2, 'A'],
[2, 1, 3, 'A']
]
grouped = itertools.groupby(data, lambda x: x[0])
results = dict()
this, last = None, None
for key, group in grouped:
results[key] = 0
for c, d in enumerate(group):
this = d
streak = c == 0 or this[3] != last[3]]
if streak:
results[key] += 1
last = this
print results
这会产生;
{1: 3, 2: 1}
答案 1 :(得分:0)
def count():
love = []
love1 = []
streak = -1
k = 0
session = 1
subject = raw_input("What is your subject? ")
trials = raw_input("How many trials do you wish to do? ")
trial = 0
for i in range(int(trials)):
choice = raw_input("What was the choice? ")
love.append(choice)
love1.append(choice)
trial += 1
print subject, trial, choice
if love[i] == love1[i-1]:
streak += 1
print subject, session, streak
这可能是你想要做多少次试验以及你的主题是什么以及如果有条纹它会增加一个。条纹从-1开始的原因是因为当你输入第一个答案时,它会增加一个,因为负指数会回到自己的位置。