Python 菜鸡请教

2018-06-15 10:12:53 +08:00
 wsds

有这么一堆数据:

#test.log
a, 1.324171
b, 0.000126
c, 1.970941
a, 1.469649
b, 0.000124
c, 0.512929
a, 1.290920
b, 0.000118
c, 0.259524
a, 0.495958
b, 0.000123
c, 0.910949
a, 1.268038
b, 0.000118
c, 1.016419
a, 1.856081
b, 0.000120
c, 1.400075
a, 1.314131
b, 0.000140

想要用 python 把左边的 key 一样的合并,但 value 要取它所有的和,还有平均值

搞了半天,发现搞不定,也是尴尬,以下还是个半成品,搞不下去了,报错,求大神指点一些简单方法

def two(file):

	arr = []
	with open(file, "r", encoding="utf-8") as f:
		for i in f.readlines():
			a = i.replace("\n", '').strip()
			if a.split(",")[0] not in arr:
				arr.append(a.split(",")[0])
	ser = -1
	while True:
		ser += 1
		try:
			if a.split(",")[ser] == arr[ser]:
				print(a.split(",")[ser])
		except IndexError:
			print("end!")
			break


two("test.log")

4429 次点击
所在节点    Python
47 条回复
linxzh1989
2018-06-15 10:16:56 +08:00
pandas groupby sum?
aborigine
2018-06-15 10:18:07 +08:00
了解一下 pandas ?导入生成个 dataframe 就行了
wsds
2018-06-15 10:20:14 +08:00
@aborigine 要用纯 python 处理,该怎么弄?
wsds
2018-06-15 10:20:26 +08:00
@aborigine 我的意思是,不要用这种一步到位的库
deepreader
2018-06-15 10:21:27 +08:00
Dictionary 会用么?
wsds
2018-06-15 10:23:12 +08:00
@deepreader 抱歉,字典我知道,但以我的水平真心实现不了
mentalkiller
2018-06-15 10:25:19 +08:00
@wsds #6 dict 是 python 内置的数据结构啊,不需要你自己实现
wsds
2018-06-15 10:26:08 +08:00
@mentalkiller 我的意思不是实现字典的功能,我的意思是用字典,我实现不了我的需求
hahastudio
2018-06-15 10:28:58 +08:00
lixm
2018-06-15 10:30:15 +08:00
ybping
2018-06-15 10:30:23 +08:00
dict 了解一下
scriptB0y
2018-06-15 10:30:33 +08:00
In [18]: data = """
...: a, 1.324171
...: b, 0.000126
...: c, 1.970941
...: a, 1.469649
...: b, 0.000124
...: c, 0.512929
...: a, 1.290920
...: b, 0.000118
...: c, 0.259524
...: a, 0.495958
...: b, 0.000123
...: c, 0.910949
...: a, 1.268038
...: b, 0.000118
...: c, 1.016419
...: a, 1.856081
...: b, 0.000120
...: c, 1.400075
...: a, 1.314131
...: b, 0.000140
...: """

In [19]: result = {}
...: for line in data.splitlines():
...: if not line: continue
...: key, value = line.split(",")
...: result.setdefault(key, []).append(float(value))
...:

In [20]: for key, values in result.items():
...: print(f"{key}: avg: {sum(values) / len(values)}, sum: {sum(values)}")
...:
a: avg: 1.2884211428571428, sum: 9.018948
b: avg: 0.00012414285714285714, sum: 0.000869
c: avg: 1.0118061666666667, sum: 6.070837

https://gist.github.com/laixintao/f4a186cea6c28fcf3dc696100458c410
wsds
2018-06-15 10:33:08 +08:00
@hahastudio
@lixm
@scriptB0y
非常感谢各位!学习了
wplct
2018-06-15 10:35:15 +08:00
def two(file):
data = {}
with open(file, "r") as f:
while True:
s = f.readline()
if s is None or not s:
break
print(s.split(', '))
k, v = s.split(', ')
v = float(v)
if k not in data:
data[k] = {
'num': 1,
'sum': v,
'avg': v
}
else:
data[k]['num'] += 1
data[k]['sum'] += v
data[k]['avg'] = data[k]['sum'] / data[k]['num']
print(data)
two('test.txt')
wsds
2018-06-15 10:40:48 +08:00
@wplct 感谢
araraloren
2018-06-15 10:43:41 +08:00
一行脚本拯救你

perl -nE 'state %z; my @z = split(", "); $z{@z[0]} += @z[1]; END { say for %z; } ' < test.log
a
9.018948
b
0.000869
c
6.070837

perl6 -ne 'state %z; given .split(", ") { %z{.[0]} += .[1].Rat; }; END { say %z; }' < test.log
{a => 9.018948, b => 0.000869, c => 6.070837}
bufpay
2018-06-15 10:51:56 +08:00
其实一个 for 循环就可以了
E1n
2018-06-15 11:03:54 +08:00
@araraloren perl 好用,用 awk 能实现吗。。
araraloren
2018-06-15 11:05:43 +08:00
@E1n awk 肯定能实现,不过我只懂基本的 awk 脚本
imagechans
2018-06-15 11:22:38 +08:00
def stand(file):
datas = [str(line).replace("\n","").strip().split(',')[1] for line in open(file)]
s = sum([float(d) for d in datas])
m = s / len(datas)
print(s,m)


stand("test.log")

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/463296

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX