如何使用 Python 循环遍历列中的行并对其进行计数?

发布于 2024-12-08 20:01:52 字数 538 浏览 1 评论 0原文

我正在尝试使用 Python 循环访问表中的列。我的列按升序排序。

我试图循环遍历行,当列中的值发生变化时,我想获取所有这些值的计数。在下面的示例列中,我要计数的第一组值是 M1。当下一行更改为 M21 时,我想对 M21 进行计数,直到它更改为 M23b 等等。

我不想使用 if/else 语句,因为有几百个不同的可能值。我在 itertools 模块中使用了 groupby 函数,但我无法解决在我的示例中使用的语法。我还尝试了一个愚蠢的循环,执行类似 if row != row.next(): do_something 的操作,但这在我脸上爆炸了。如果有人可以建议解决方法或向我展示一个可以为我完成此操作的示例脚本,我将不胜感激。

示例栏:

M1
M1
M1
M21
M21
M23b
M23b
S2
S23b
S23B
O1
O2
O2
O2

I am trying to loop through column in an access table using Python. The column I have is sorted ascending.

I am trying to loop though the rows and when the value in the column changes, I want to get count of of all those values. In the example column below, the first group of values I want to count are the M1's. When the next row changes to M21, I want to count the M21's until it changes to M23b and so on.

I don't want to use an if/else statement, because there are a few hundred different possible values. I played around with the groupby function in the itertools module, but I haven't been able to solve the syntax to work in my example. I also tried a silly loop doing something like if row != row.next(): do_something but that blew up in my face. If anyone can suggest a work-around or show me a sample script that will do this for me, I'd appreciate it.

Example Column:

M1
M1
M1
M21
M21
M23b
M23b
S2
S23b
S23B
O1
O2
O2
O2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

挽手叙旧 2024-12-15 20:01:52

您使用 itertools.groupby 的直觉是正确:

for key, group in groupby(column):
    count = sum(1 for item in group) # Thanks JBernardo
    # the key is what is in the column, count is the number of items

或者,如果您需要的只是计数,则很简单:

from collections import Counter # Python 2.7+

group_counts = Counter(column)

您可以实现 Counter as:

from collections import defaultdict:

group_counts = defaultdict(int)

for item in column:
    group_counts[item] += 1

在旧版本的 Python 上。

Your intuition to use itertools.groupby was correct:

for key, group in groupby(column):
    count = sum(1 for item in group) # Thanks JBernardo
    # the key is what is in the column, count is the number of items

Alternatively, if all you need is the counts, it's as simple as:

from collections import Counter # Python 2.7+

group_counts = Counter(column)

You can implement Counter as:

from collections import defaultdict:

group_counts = defaultdict(int)

for item in column:
    group_counts[item] += 1

on older versions of Python.

避讳 2024-12-15 20:01:52

如果您想在执行其他工作的循环中添加打印,以下内容可能会有所帮助:

from collections import Counter  # or defaultdict

col_counts = Counter()           # or defaultdict(int)

last_value = object()            # won't show up in table
for row in access_table:
    col_counts[row[field]] += 1
    if row[field] != last_value:
        print(col_counts[last_value])
        last_value = row[field]
    ...
    other_processing()
    ...

If you want to add printing in a loop doing other work, the following may be helpful:

from collections import Counter  # or defaultdict

col_counts = Counter()           # or defaultdict(int)

last_value = object()            # won't show up in table
for row in access_table:
    col_counts[row[field]] += 1
    if row[field] != last_value:
        print(col_counts[last_value])
        last_value = row[field]
    ...
    other_processing()
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文