我正在尝试将列表项更改为实际值。例如,这就是我的数据现在的样子:
data = [
['student1', '"answer 1"', '"answer 2"', '"answer 3"'],
['student2', '"answer 10"', '"answer 2"', '"answer 3", "answer 4"']
如数据[1]中的最后一项所示,对单个问题可以有多个回答。我现在的代码是:
def answer_replace(data):
replaced_data = []
for element in data:
replaced_element = []
for index, item in enumerate(element):
print (index)
print (item)
if index in answer_replacement:
replaced_element.append(answer_replacement[index].get(item, item))
else: # no replacement available for this index
replaced_element.append(item)
print (replaced_element)
replaced_data.append(replaced_element)
return replaced_data
除了有多个答案的情况外,它工作正常。我试图使用re.sub,但无法让它工作。 (我想要更改值的答案存储在字典answer_replacement中)
答案 0 :(得分:0)
我相信你想删除"回答"来自学生的回应。你可以试试这个:
data = [ ['student1', "answer 1", "answer 2", "answer 3"], ['student2', "answer 10", "answer 2", "answer 3", "answer 4"]]
student_data = {i[0]:[b.replace("answer", "replacement") for b in i[1:]] for i in data}
输出:
{'student2': ['replacement 10', 'replacement 2', 'replacement 3', 'replacement 4'], 'student1': ['replacement 1', 'replacement 2', 'replacement 3']}
答案 1 :(得分:0)
在发布此消息后的几分钟内完成了计算。去搞清楚。这是我做的:
UseSignalR()