python - 如何将相应的索引附加到列表
问题描述
我使用 for 循环和解包元组将一组学生过滤到三个存储桶中。我怎样才能将他们相应的学号附加到每个分数上?谢谢你。
#create index for 100 students, starting with 1
student_index = list(range(1,101))
#join index with results sheet
student_score_index = list(zip(student_index, results_sheet2))
group_a = []
group_b = []
group_c = []
# Iterate over pairs
for index, pair in enumerate(student_score_index):
# Unpack pair: index, student_score
index, score = pair
# assign student scores into 3 buckets: group_a,group_b,group_c
if score >= 60:
group_a.append(score)
elif score >= 50 and score <=59:
group_b.append(score)
else:
group_c.append(score)
print(group_a)
[61, 67, 63, 62, 62, 62]
对于所有三个组,所需的结果应该是这样的:
#print corresponding student index number, score
group_a = [(29,61),(51,67),(63,63),(65,62),(98,62),(99,62)]
解决方案
我不知道 score 或 group_a 等是什么......所以,这里我有一个例子 -
group_a = [61, 67, 63, 62, 62, 62]
score = [29,51,63,65,98,99,62]
new_lst = []
for i,j in zip(group_a,score):
new_lst.append((j,i))
print(new_lst)
结果:
[(29, 61), (51, 67), (63, 63), (65, 62), (98, 62), (99, 62)]
所以,你可以在你的代码中实现它
推荐阅读
- css - 反应原生布局 ScrollView over View
- java - 在 JPA hibernate 中,如何在不引用外键的情况下从父表和子表中获取数据?
- c# - 如何以编程方式添加 ResourceDictionary?
- java - 从 ListView 添加游标数据并存储在数组中
- asp.net - ASP.NET/实体框架格式字符串
- javascript - 在 JavaScript 函数中将 JavaScript var 传递给 PHP
- c# - ARCore 2D 对象未正确粘附到锚点
- javascript - Vue“v-for”仅显示最后一次迭代(在 v-select 中)
- sql-server - SQL query to Linq lambda
- sql - 在 SQL Server 中更新记录之前制作记录副本的正确方法是什么?