根据Log表的data字段计算每个玩家的总得分

需求:

线上数据库的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

根据Log表的data字段计算每个玩家的总得分的相关文章

[数据库] 取指定表中某字段的累加和不超过总和80%的行

有表 Table_1, 字段 Value int,  P float .5 要取出以 Value 字段倒序的 P 字段累加和 不超过 整个表中P字段总和的 80%的行.  并在返回列表中 加入字段 SUM ,存放当前行与前面所有行的累加和. 折腾了半天, 写了下面的查询sql: declare @e float select @e = sum(P) from [Table_1] Set @e = @e * 0.8 ;with T as ( select [ID] = row_number() o

在arcgis使用python脚本进行字段计算时对中文的处理方案

一.引言 在arcgis打开一个图层的属性表,可以对属性表的某个字段进行计算,但是在平常一般都是使用arcgis提供的字段计算器的界面进行傻瓜式的简答的赋值操作,并没有使用到脚本对字段值进行逻辑的操作.由于最近一直在学python脚本,刚好又碰上一好基友需要我的助攻(使用arcgis制图),这就用上了.本以为能够轻松搞定的,没想到搬石头砸脚了,下面就来说我是如何被砸脚的吧. 二.问题描述:将test字段中值为“湖南”的变为“湖南省”. 这个逻辑是相当的简单,使用python写一个对应的方法为:

oracle表名、字段名等对象的命名长度限制

原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/45854385 今天在为某系统数据库结构整理升级脚本时,遇到了"命名字节过长的错误",类似于下面的截图语句: 由于升级的结构中对于字段名的命名根据业务进行了修改,出现了命名过长的情况. 这里想说一下,对于oracle,表名.字段名等对象命名字节个数限制在

[C#]使用WebClient上传文件并同时Post表单数据字段到服务端

转自:http://www.97world.com/archives/2963 之前遇到一个问题,就是使用WebClient上传文件的同时,还要Post表单数据字段,一开始以为WebClient可以直接做到,结果发现如果先Post表单字段,就只能获取到字段及其值,如果先上传文件,也只能获取到上传文件的内容.测试了不少时间才发现WebClient不能这么使用. Google到相关的解决思路和类,因为发现网上的一些文章不是介绍得太简单就是太复杂,所以这里简单整理一下,既能帮助自己巩固知识,也希望能够

数据库指定库表中的字段进行备份,并以表格的形式定时发送邮件到指定邮箱 ?

备份数据进行定时邮件发送 mkdir -p /opt/module/shell/ mkdir -p /opt/module/shell/sql/revenue.sql mkdir -p /opt/module/shell/csv/revenue #MailTool.jar 放在指定目录下 便于发送邮件使用 可自动生成表格 vim /opt/module/shell/daily_text.sh # !/bin/bash #数据库指定库.表中的字段进行备份,并以表格的形式发送邮件到指定邮箱 #定义变

MySQL Workbench 创建数据库,添加新表,添加字段

建立数据库 第一步: 第二步: 第三步: 如图弹出弹框,继续点击Apply按钮,最后点击Finish按钮完成数据库的建立 创建表与添加字段 双击!!!  一下刚刚建立好的数据库,然后再创建表,不然会出错,右键点击Tables 然后点击Create new tables ,填写表名,以及字段的信息,之后点击  apply ,一张表就建完了. PK: primary key (column is part of a pk)  主键 NN: not null (column is nullable) 

06 | 全局锁和表锁 :给表加个字段怎么有这么多阻碍?

今天我要跟你聊聊MySQL的锁.数据库锁设计的初衷是处理并发问题.作为多用户共享的资源,当出现并发访问的时候,数据库需要合理地控制资源的访问规则.而锁就是用来实现这些访问规则的重要数据结构. 根据加锁的范围,MySQL里面的锁大致可以分成全局锁.表级锁和行锁三类.今天这篇文章,我会和你分享全局锁和表级锁.而关于行锁的内容,我会留着在下一篇文章中再和你详细介绍. 这里需要说明的是,锁的设计比较复杂,这两篇文章不会涉及锁的具体实现细节,主要介绍的是碰到锁时的现象和其背后的原理. 全局锁 顾名思义,全

EasyUi获取表单所有字段和相应值,非官方form提交

js: // 定义变量var formData = {}; // 获取表单所有字段和相应值 装填到formData变量中 var fields = $("[data-field]"); fields.each(function (index, item) { if ($(item).hasClass("textbox-f")) { $(item).next().find(".textbox-value").each(function (index

mysql,user表中各字段的含义

1.查询user表 1 select * from mysql.user 2.user表中各字段的含义 1 Select_priv:用户可以通过SELECT命令选择数据. 2 Insert_priv:用户可以通过INSERT命令插入数据; 3 Update_priv:用户可以通过UPDATE命令修改现有数据; 4 Delete_priv:用户可以通过DELETE命令删除现有数据; 5 Create_priv:用户可以创建新的数据库和表; 6 Drop_priv: 用户可以删除现有数据库和表; 7