【发布时间】:2020-07-02 06:22:22
【问题描述】:
我在 python 中生成了一个调查,现在我有一个答案列表,我想将其与包含评估键的字典匹配。
我没有实现遍历列表 (answers_all),将一个元素与字典 (eval_keys) 值匹配并打印与该值关联的键。
这里是python代码:
import random
def rand_participant(n):
#generate a single random participant
age = random.randint(28,35)
gender = random.choice(['male', 'female'])
return int(age), str(gender)
#generate a list of 100 random participants
sample_probe = [rand_participant(1) for _ in range(101)]
def rand_answer(x):
#create a single random answer
f = random.randint(1,5)
q = random.randint(1,5)
return int(f), int(q)
def participant_answer(y):
# create all 6 random answers
all_answer = [rand_answer(1) for blu in range(6)]
return all_answer
def survey_res(a):
# create set of random survery results
result = [participant_answer(1) for bla in range(101)]
return result
my_survey = list(zip(sample_probe, survey_res()))
answer_all = list(survey_res(1))
answer_all =
[[(3, 2), (5, 5), (1, 5), (5, 5), (5, 1), (5, 5)],
[(4, 3), (1, 2), (5, 3), (5, 4), (1, 1), (4, 1)],
[(2, 1), (4, 1), (5, 2), (2, 3), (4, 1), (3, 2)],
[(3, 4), (1, 1), (1, 3), (5, 4), (5, 5), (1, 1)],
[(1, 2), (3, 3), (4, 3), (2, 4), (1, 3), (1, 1)],
[(3, 1), (5, 5), (4, 4), (2, 5), (2, 3), (3, 3)],
[(2, 3), (4, 1), (3, 1), (5, 2), (2, 3), (1, 3)],
[(3, 4), (4, 2), (5, 2), (5, 5), (1, 1), (4, 4)],
[(5, 5), (5, 2), (2, 2), (3, 2), (2, 2), (4, 1)],
[(4, 1), (1, 1), (3, 1), (3, 4), (4, 5), (1, 4)],
[(2, 1), (5, 3), (2, 2), (1, 4), (3, 1), (5, 1)],
[(2, 1), (3, 5), (3, 4), (4, 3), (4, 2), (3, 1)],
[(5, 3), (1, 2), (2, 5), (4, 5), (3, 1), (5, 3)]
...
]
eval_keys = {
"Q": (1,1), "A": (1,2), "A": (1,3), "A": (1,4), "P": (1,5),
"R": (2,1), "Q": (2,2), "I": (2,3), "I": (2,4), "M": (2,5),
"R": (3,1), "I": (3,2), "I": (3,3), "I": (3,4), "M": (3,5),
"R": (4,1), "I": (4,2), "I": (4,3), "Q": (4,4), "M": (4,5),
"R": (5,1), "R": (5,2), "R": (5,3), "R": (5,4), "Q": (5,5)
}
列表的每一行都是单个参与者的答案。因此,列表中每一行的输出应为 6 个字母(如“R”或“Q”等)。
【问题讨论】:
-
"这里有一些代码:"您刚刚添加了数据,但忘记粘贴代码了。
标签: python list dictionary matching