如何在嵌套列表中汇总学时

发布于 2025-01-20 05:37:25 字数 635 浏览 1 评论 0原文

我有一个嵌套数组。我需要获得学时的总和。学时分别处于[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情哥ㄟ 2025-01-27 05:37:25

学分分别位于位置 [2] 和 [5]。

因此,我假设您只想对索引 25 处的 integers 求和。

[ "MR. JONES", "ACCT203", 2, 3.0, "CIS100", 3, 2.5 ]

要从嵌套的 list 中获取单个 list,您只需从其索引中获取它,例如:

>>> marks[0] # ...will give you...
[ "MR. JONES", "ACCT203", 2, 3.0, "CIS100", 3, 2.5 ]

所以现在您只需将其 2< /code>nd 和 5th 位置,例如:

>>> marks[0][2] + marks[0][5]
5

如果你想收集所有的总和,你只需要使用 list 理解...

list_of_the_sums_of_the_marks = [mark[2] + mark[5] for mark in marks]

...并且然后总结一下......

sum(list_of_the_sums_of_the_marks)

我建议你阅读一些关于嵌套列表

The credit hours are in position [2] and [5] respectively.

So I assume you only want to sum the integers at indexes 2 and 5.

[ "MR. JONES", "ACCT203", 2, 3.0, "CIS100", 3, 2.5 ]

To get a single list from a nested list, you just have to take it from its index, for example:

>>> marks[0] # ...will give you...
[ "MR. JONES", "ACCT203", 2, 3.0, "CIS100", 3, 2.5 ]

So now you just have to sum its 2nd and 5th positions, for example:

>>> marks[0][2] + marks[0][5]
5

If you want to gather all the sums, you just have to use a list comprehension...

list_of_the_sums_of_the_marks = [mark[2] + mark[5] for mark in marks]

...and then to sum it...

sum(list_of_the_sums_of_the_marks)

I would suggest you to read something about nested lists.

迷路的信 2025-01-27 05:37:25

如果您想要总和,请执行此操作:

total = sum([mark[2] + mark[5] for mark in marks])
print(total)
32

或者如果您想要一个不错的元素中的每个元素的总和:

results = {mark[0]: mark[2] + mark[5] for mark in marks}
print(results)
{'MR. JONES': 5, 'SKARE EEEY': 4, 'HALO WEEN': 7, 'BOB KATZ': 8, 'ANNIE BANANE': 8}

Just do this if you want the total sum:

total = sum([mark[2] + mark[5] for mark in marks])
print(total)
32

Or if you want the sum of every element in a nice dict:

results = {mark[0]: mark[2] + mark[5] for mark in marks}
print(results)
{'MR. JONES': 5, 'SKARE EEEY': 4, 'HALO WEEN': 7, 'BOB KATZ': 8, 'ANNIE BANANE': 8}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文