我已经在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”等)。
您的eval_keys
字典有问题,因为它包含许多相同的键。程序一旦编译,所有重复项将被删除,并且每个唯一键仅保留一个键值对。它应该以相反的方式构造,以元组值作为键,字母字符串作为值。此外,所有函数都有不需要的不必要参数,并且有一些多余的强制转换为int和str。现在,为了将answer_all
中的每个元组列表转换为6个字母的字符串,您可以将元素映射到eval_keys
并将它们连接在一起。
import random
def rand_participant():
#generate a single random participant
age = random.randint(28,35)
gender = random.choice(['male', 'female'])
return age, gender
def rand_answer():
#create a single random answer
f = random.randint(1,5)
q = random.randint(1,5)
return f, q
def participant_answer():
# create all 6 random answers
all_answer = [rand_answer() for blu in range(6)]
return all_answer
def survey_res():
# create set of random survery results
result = [participant_answer() for bla in range(101)]
return result
#generate a list of 100 random participants
sample_probe = [rand_participant() for _ in range(101)]
my_survey = list(zip(sample_probe, survey_res()))
answer_all = list(survey_res())
eval_keys = {
(1,1): "Q", (1,2): "A", (1,3): "A", (1,4): "A", (1,5): "P",
(2,1): "R", (2,2): "Q", (2,3): "I", (2,4): "I", (2,5): "M",
(3,1): "R", (3,2): "I", (3,3): "I", (3,4): "I", (3,5): "M",
(4,1): "R", (4,2): "I", (4,3): "I", (4,4): "Q", (4,5): "M",
(5,1): "R", (5,2): "R", (5,3): "R", (5,4): "R", (5,5): "Q"
}
answers = [''.join(map(lambda x: eval_keys[x], ans)) for ans in answer_all]
print(answers)