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")

4678 次点击
所在节点    Python
47 条回复
UnluckyNinja
2018-06-15 21:49:22 +08:00
def text = '''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
'''

// file.readLines().collect{
text.readLines().collect{
it.split(',')*.trim()
}.groupBy{
it[0]
}.collectEntries{k, vList ->
[(k): [sum: def sum = vList.sum{ it[1] as BigDecimal }, average: sum / vList.size()]]
}

/* result: [a:[sum:9.018948, average:1.2884211429], b:[sum:0.000869, average:0.0001241429], c:[sum:6.070837, average:1.0118061667]]

groovy 写的,groovy 有的 python 肯定有,语法方法名啥的改一下应该就差不多了 */
Binb
2018-06-16 10:16:44 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-

a = {}
with open('test.log','r') as f:
for i in f:
j = i.split(',')
a.setdefault(j[0],[]).append(float(j[1]))

result = {}
for k,v in a.items():
s = sum(v)
result.setdefault(k,[]).append(s)
result[k].append(s/len(v))

print result
# {'a': [9.018948, 1.2884211428571428], 'c': [6.070837, 1.0118061666666667], 'b': [0.000869, 0.00012414285714285714]}
NICCEEEE
2018-06-16 17:21:39 +08:00
import collections

d = """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"""

L = [(i[0], i[3:]) for i in d.split('\n')]
data_dict = collections.defaultdict(int)

for i, j in L:
data_dict[i] += float(j)

print(data_dict)
yangxiaoyong
2018-06-16 20:33:05 +08:00
讲下原理的东西,map reduce 可以了解一下,首先把数据分组归类
map (lambda x: { value: x.key, key: x.key, count: 1})
按上面的把数据按 key 分组放好
然后执行归约函数,将数据集合归约为一个最终结果
reduce(lambda acc, curr: merge(acc,curr), mapdata )

merge 根据 key 将相同 key 的数值相加得到总合,count 相加得到次数,总和除以次数可以得平均值

最后的结果应该是 {a: { value,count,avg}}

手机码字,凑合看吧
bugcoder
2018-06-17 08:24:41 +08:00
没人贴 pandas 的,我就献个丑吧:
···
import pandas as pd

data_file = 'data.txt'
data_df = pd.read_csv(data_file, comment='#', names=['key', 'value'])

sums = data_df.groupby('key').sum()
means = data_df.groupby('key').mean()
···
mingyun
2018-07-15 11:55:11 +08:00
@bugcoder 这个简单
biglazycat
2020-09-06 22:09:24 +08:00
convert_list = {}
for line in open('test.log'):
k, v = line.split(',')
convert_list.setdefault(k,[]).append(float(v.strip()))

# print(convert_list)
for k, v in convert_list.items():
total_sum = sum(v)
avg = total_sum / len(v)
print(total_sum)
print(avg)

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

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

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

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

© 2021 V2EX