Python 循环遍历切片列表并将元素与dict值匹配
我已经用python生成了一个调查,现在我有了一个答案列表,我想将其与包含评估键的字典相匹配 我无法遍历列表(answers\u all),获取一个元素,将其与字典(eval\u keys)值匹配,并打印与该值关联的键 以下是python代码:Python 循环遍历切片列表并将元素与dict值匹配,python,list,dictionary,matching,Python,List,Dictionary,Matching,我已经用python生成了一个调查,现在我有了一个答案列表,我想将其与包含评估键的字典相匹配 我无法遍历列表(answers\u all),获取一个元素,将其与字典(eval\u keys)值匹配,并打印与该值关联的键 以下是python代码: import random def rand_participant(n): #generate a single random participant age = random.randint(28,35) gender =
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_键
字典有问题,因为它包含许多相同的键。一旦程序被编译,所有重复项都将被删除,并且每个唯一键只保留一个键、值对。它应该以相反的方式构造,元组值作为键,字母字符串作为值。此外,您的所有函数都有不必要的未使用的参数,并且有一些多余的转换为int和str。现在,为了将answer\u all
中的每个元组列表转换为一个由6个字母组成的字符串,您可以将元素映射到eval\u 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)
下面是一些代码:“您刚刚添加了数据,但忘记粘贴代码。