python读文件统计次数和求和怎么写?
来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/08/03 10:38:07
python读文件统计次数和求和怎么写?
有一个文件文件内容如下:
A 1 22
A 1 44
A 2 33
B 1 22
B 3 44
B 4 55
C 1 11
C 4 22
.
文件中有三列第一列代表ID,第二列代表类型,第三列代表价格,类型和价格均不固定
我现在想统计
通过第一列和第二列分组求和的值和第一列、第二列分组后第三列的count值
例如
A 1 2 66
A 2 1 33
.
我知道可以用collection的count统计出A第二列代表1的有几个数2有几个
但是不知道怎么分组后求第三列的和
有一个文件文件内容如下:
A 1 22
A 1 44
A 2 33
B 1 22
B 3 44
B 4 55
C 1 11
C 4 22
.
文件中有三列第一列代表ID,第二列代表类型,第三列代表价格,类型和价格均不固定
我现在想统计
通过第一列和第二列分组求和的值和第一列、第二列分组后第三列的count值
例如
A 1 2 66
A 2 1 33
.
我知道可以用collection的count统计出A第二列代表1的有几个数2有几个
但是不知道怎么分组后求第三列的和
![python读文件统计次数和求和怎么写?](/uploads/image/z/16366770-18-0.jpg?t=python%E8%AF%BB%E6%96%87%E4%BB%B6%E7%BB%9F%E8%AE%A1%E6%AC%A1%E6%95%B0%E5%92%8C%E6%B1%82%E5%92%8C%E6%80%8E%E4%B9%88%E5%86%99%3F)
#!/usr/bin/env python
# coding: utf-8
import re
patt = re.compile(r"""
(?P<id>\S+)
\s+
(?P<category>\S+)
\s+
(?P<number>\d+)
""", re.I|re.U|re.X)
context = """
A 1 22
A 1 44
A 2 33
B 1 22
B 3 44
B 4 55
C 1 11
C 4 22
"""
collects = {}
for m in filter(None, map(patt.match, context.splitlines())):
d = m.groupdict()
collects.setdefault((d["id"], d["category"]), []).append(int(d["number"]))
for (id, category) in sorted(collects):
print id, category, sum(collects[(id, category)])>python -u "baidu.py"
A 1 66
A 2 33
B 1 22
B 3 44
B 4 55
C 1 11
C 4 22
>Exit code:0 Time:0.065
再问: python 3写的? 我2.7的环境运行不了 还有如果是读文本,而且文本里面是以|分割怎么办?
再答: 是python2的版本; 将正则表达式中的\s调整一下即可适应|分隔符号;$ cat data.txt
A|1|22
A|1|44
A|2|33
B|1|22
B|3|44
B|4|55
C|1|11
C|4|22
$ python
Python 2.7.2+ (default, Jul 20 2012, 22:12:53)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ python reparser.py
A -- 1, count:2, summary:66
A -- 2, count:1, summary:33
B -- 1, count:1, summary:22
B -- 3, count:1, summary:44
B -- 4, count:1, summary:55
C -- 1, count:1, summary:11
C -- 4, count:1, summary:22
#!/usr/bin/env python
# coding: utf-8
import re
patt = re.compile(r"""
(?P<id>\S+)
[\s\|]+
(?P<category>\S+)
[\s\|]+
(?P<number>\d+)
""", re.I|re.U|re.X)
collects = {}
with open("data.txt", 'rt') as handle:
for m in filter(None, map(patt.match, handle)):
d = m.groupdict()
collects.setdefault((d["id"], d["category"]), []).append(int(d["number"]))
# coding: utf-8
import re
patt = re.compile(r"""
(?P<id>\S+)
\s+
(?P<category>\S+)
\s+
(?P<number>\d+)
""", re.I|re.U|re.X)
context = """
A 1 22
A 1 44
A 2 33
B 1 22
B 3 44
B 4 55
C 1 11
C 4 22
"""
collects = {}
for m in filter(None, map(patt.match, context.splitlines())):
d = m.groupdict()
collects.setdefault((d["id"], d["category"]), []).append(int(d["number"]))
for (id, category) in sorted(collects):
print id, category, sum(collects[(id, category)])>python -u "baidu.py"
A 1 66
A 2 33
B 1 22
B 3 44
B 4 55
C 1 11
C 4 22
>Exit code:0 Time:0.065
再问: python 3写的? 我2.7的环境运行不了 还有如果是读文本,而且文本里面是以|分割怎么办?
再答: 是python2的版本; 将正则表达式中的\s调整一下即可适应|分隔符号;$ cat data.txt
A|1|22
A|1|44
A|2|33
B|1|22
B|3|44
B|4|55
C|1|11
C|4|22
$ python
Python 2.7.2+ (default, Jul 20 2012, 22:12:53)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ python reparser.py
A -- 1, count:2, summary:66
A -- 2, count:1, summary:33
B -- 1, count:1, summary:22
B -- 3, count:1, summary:44
B -- 4, count:1, summary:55
C -- 1, count:1, summary:11
C -- 4, count:1, summary:22
#!/usr/bin/env python
# coding: utf-8
import re
patt = re.compile(r"""
(?P<id>\S+)
[\s\|]+
(?P<category>\S+)
[\s\|]+
(?P<number>\d+)
""", re.I|re.U|re.X)
collects = {}
with open("data.txt", 'rt') as handle:
for m in filter(None, map(patt.match, handle)):
d = m.groupdict()
collects.setdefault((d["id"], d["category"]), []).append(int(d["number"]))
python 统计单词平均长度,统计a出现的次数
从文件中读入一段英语文章,统计文章中单词的数量,求出现次数最多和最少的单词和数量.用C怎么实现?
用python怎么抽取中间“>”和“
python统计个单词数目
如何用python统计一篇英语文章里单词总数和句子总数
python如何统计列表的长度
excel2007怎么统计出一列中各个值出现的次数和比例并以饼图表示出来
excel,A-M列数字,n1和o1填入数字,所对应的两列每行两个数求和取最大数在p1显示,在q1统计最大数次数.
java写一个程序统计文件中一篇文章每个词(要中文和英语的都可以的)的个数
怎么用excel做数据统计生成图表?例如分数的统计,怎么做出每个分数出现的次数?
请问怎么用excel做数据统计生成图表?例如分数的统计,怎么做出每个分数出现的次数?
java编程 写一段程序统计一段字符串中每一个单词的出现次数 并按照次数的倒序输出