【发布时间】:2017-06-04 19:42:13
【问题描述】:
我正在尝试根据用户数据创建列表。数据是 16 个问题的调查,因此我想将数据拆分为 16 个答案的列表,或者只是 answers = ['No data available] 的列表。到目前为止,我的代码如下所示。
studentanswers = []
contains_data = False
temp = []
for i in answers:
if i == 'No data available':
if contains_data:
studentanswers.append(temp)
temp = []
studentanswers.append([i])
contains_data = False
else:
studentanswers.append([i])
else:
contains_data = True
if len(temp) < 16:
temp.append(i)
else:
studentanswers.append(temp)
temp = []
contains_data = False
但是,随着我的迭代,数据的计数不正确,并且一些数据被分配给了错误的用户。例如,学生 1 的第 16 题将变为学生 2 的第 1 题。
【问题讨论】:
-
不清楚你在问什么。请分享示例输入和预期输出。
标签: python list python-3.x append