如何在嵌套列表中汇总学时
我有一个嵌套数组。我需要获得学时的总和。学时分别处于[2]和[5]。如何使用Python中的循环来实现这一目标?我不是在numpy中熟悉的。
marks = [
[ "MR. JONES", "ACCT203", 2, 3.0, "CIS100", 3, 2.5 ],
[ "SKARE EEEY", "ACCT203", 2, 2.5, "BUS123", 2, 3.0 ],
[ "HALO WEEN", "ACCT300", 5, 2.0, "ACCT301", 2, 1.5 ],
[ "BOB KATZ", "ACCT300", 5, 1.0, "BUS278", 3, 4.0 ],
[ "ANNIE BANANE", "ACCT300", 5, 0.0, "CIS223", 3, 1.5 ],
]
我要做的就是打印它们。我超越了这个。
for credit_hours in marks:
cred_hours_part_one = credit_hours[2]
cred_hours_part_two = credit_hours[5]
print(cred_hours_part_one)
print(cred_hours_part_two)
I have a nested array. I need to get the sum of credit hours. The credit hours are in position [2] and [5] respectively. How do I achieve this using a for loop in python? I'm not conversant in Numpy.
marks = [
[ "MR. JONES", "ACCT203", 2, 3.0, "CIS100", 3, 2.5 ],
[ "SKARE EEEY", "ACCT203", 2, 2.5, "BUS123", 2, 3.0 ],
[ "HALO WEEN", "ACCT300", 5, 2.0, "ACCT301", 2, 1.5 ],
[ "BOB KATZ", "ACCT300", 5, 1.0, "BUS278", 3, 4.0 ],
[ "ANNIE BANANE", "ACCT300", 5, 0.0, "CIS223", 3, 1.5 ],
]
All I have managed to do is print them. I'm stuck beyond this.
for credit_hours in marks:
cred_hours_part_one = credit_hours[2]
cred_hours_part_two = credit_hours[5]
print(cred_hours_part_one)
print(cred_hours_part_two)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,我假设您只想对索引
2
和5
处的int
egers 求和。要从嵌套的
list
中获取单个list
,您只需从其索引中获取它,例如:所以现在您只需将其
2< /code>nd 和
5
th 位置,例如:如果你想收集所有的总和,你只需要使用
list
理解......并且然后总结一下......
我建议你阅读一些关于
嵌套列表
。So I assume you only want to sum the
int
egers at indexes2
and5
.To get a single
list
from a nestedlist
, you just have to take it from its index, for example:So now you just have to sum its
2
nd and5
th positions, for example:If you want to gather all the sums, you just have to use a
list
comprehension......and then to sum it...
I would suggest you to read something about
nested lists
.如果您想要总和,请执行此操作:
或者如果您想要一个不错的元素中的每个元素的总和:
Just do this if you want the total sum:
Or if you want the sum of every element in a nice dict: