根據Log表的data字段計算每個玩家的總得分
阿新 • • 發佈:2018-01-09
結果 from split clas lba 列表 ray c# 練習 需求:
線上數據庫的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再實現一遍,以做練習。
根據Log表的data字段計算每個玩家的總得分