需求:
线上数据库的Log表的date字段记录了每个玩家在每一局的分数,现为了比较“每一局的分数和”与“score”字段的结果是否一致,需要计算每个玩家在每一局的分数总和。
分析:
mysql> select data from log where deskid=967745\G *************************** 1. row *************************** data: -12,26,-14,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466757,41515466757859;0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466827,41515466827895;5,-3,-2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466885,41515466885924;-29,22,7,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,4,1515466944,41515466944957;-2,10,-8,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466994,41515466994989;-32,-11,43,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467022,415154670221;-7,17,-10,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467074,4151546707425;14,0,-14,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467126,4151546712649;-5,-3,8,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467191,4151546719180;0,-11,11,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467249,41515467249106; 1 row in set (0.04 sec)
由以上查询结果可以看到,data字段将每一局的数据用“;”隔开。每一组数据的前三列(-12,26,-14,)代表了这一小局,三个玩家的分数。将每一组数据的第一列相加即为第一个玩家的总分数。
Linux Shell实现:
- 将data数据保存到变量data中:
# data='-12,26,-14,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466757,41515466757859;0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466827,41515466827895;5,-3,-2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466885,41515466885924;-29,22,744,41515466944957;-2,10,-8,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466994,41515466994989;-32,-11,43,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467022,415154670221;-7,17,-10,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467074,410,0,4,1515467126,4151546712649;-5,-3,8,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467191,4151546719180;0,-11,11,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467249,41515467249106' # echo $data -12,26,-14,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466757,41515466757859 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466827,41515466827895 5,-3,-2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466885,41515466885924 -29,22,744,41515466944957 -2,10,-8,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466994,41515466994989 -32,-11,43,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467022,415154670221 -7,17,-10,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467074,410,0,4,1515467126,4151546712649 -5,-3,8,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467191,4151546719180 0,-11,11,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467249,41515467249106
- 将data变量中的内容保存到数组array中,并指定“;”为分隔符:
# array=($a) # IFS=';'
- 遍历数组array,查看数组中的内容:
# for i in ${array[@]};do echo $i;done -12,26,-14,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466757,41515466757859 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466827,41515466827895 5,-3,-2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466885,41515466885924 -29,22,7,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,4,1515466944,41515466944957 -2,10,-8,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466994,41515466994989 -32,-11,43,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467022,415154670221 -7,17,-10,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467074,4151546707425 14,0,-14,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467126,4151546712649 -5,-3,8,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467191,4151546719180 0,-11,11,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467249,41515467249106
- 求每个玩家的总分数:
# for i in ${array[@]};do echo $i;done | awk -F ',' '{sum+=$1} END{print sum}' -68 # for i in ${array[@]};do echo $i;done | awk -F ',' '{sum+=$2} END{print sum}' 47 # for i in ${array[@]};do echo $i;done | awk -F ',' '{sum+=$3} END{print sum}' 21
Python实现:
- 将data数据保存到变量data中:
In [1]: data = '-12,26,-14,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466757,41515466757859;0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466827,41515466827895;5,-3,-2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466885,41515466885924;-29,22,7,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0, ...: 4,1515466944,41515466944957;-2,10,-8,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466994,41515466994989;-32,-11,43,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467022,415154670221;-7,17,-10,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467074,4151546707425;14,0,-14,0,1,0,0,0,0, ...: 0,0,0,0,0,0,0,0,0,0,0,4,1515467126,4151546712649;-5,-3,8,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467191,4151546719180;0,-11,11,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467249,41515467249106'
- 根据“;”将字符串data拆分,遍历拆分后的列表:
In [2]: for line in data.split(';'): ...: print(line) ...: -12,26,-14,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466757,41515466757859 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466827,41515466827895 5,-3,-2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466885,41515466885924 -29,22,7,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,4,1515466944,41515466944957 -2,10,-8,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515466994,41515466994989 -32,-11,43,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467022,415154670221 -7,17,-10,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467074,4151546707425 14,0,-14,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467126,4151546712649 -5,-3,8,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467191,4151546719180 0,-11,11,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1515467249,41515467249106
- 求第一个玩家的总分数:
In [3]: score1 = 0 In [4]: for line in data.split(';'): ...: score1_tmp = line.split(',')[0] ...: if score1_tmp: ...: score1 += int(score1_tmp) ...: print(score1) ...: -68
PS:
现阶段正在自学Python,但是线上业务并没有用到。只好将Shell实现的功能,用Python再实现一遍,以做练习。
原文地址:http://blog.51cto.com/13568014/2059097
时间: 2024-11-02 10:49:36