【问题标题】:looping through sliced list and match elements with dict values遍历切片列表并匹配具有 dict 值的元素
【发布时间】: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


【解决方案1】:

您的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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2020-04-28
    • 2021-06-14
    • 1970-01-01
    相关资源
    最近更新 更多