一道 python 数据分析的题目

python 数据分析的题目。

做题方法:使用 pandas 读取数据,然后分析。

知识点:pandas,正则表达式,py知识。

过程:不断使用 GPT,遇到有问题的地方自己分析,把分析的结果告诉 GPT,注意要使用 Data Analysis,中文效果不是很好。

python 复制代码
import pandas as pd
import hashlib
import re
from datetime import datetime

def load_csv(file_path):
    return pd.read_csv(file_path)

def preprocess_a_table(a_table):
    account_table_map = {}
    for _, row in a_table.iterrows():
        account = row['账号']
        table = row['操作表']
        if account not in account_table_map:
            account_table_map[account] = set()
        account_table_map[account].add(table)
    return account_table_map

#  row 是b表
def is_valid_operation(row, a_table, account_table_map):
    account = row['账号']
    operation = row['执行操作'].lower()

    # Extracting table name and operation type
    table_name_match = re.search(r'from (\w+)', operation) or re.search(r'into (\w+)', operation) or re.search(r'update (\w+)', operation)
    table_name = table_name_match.group(1) if table_name_match else None

    operation_type_match = re.match(r'(\w+)', operation)
    operation_type = operation_type_match.group(1) if operation_type_match else None

    # Check if account is not in account_table_map or if the table_name is not in the account's permissible tables
    if account not in account_table_map or (table_name and table_name not in account_table_map[account]):
        return '0_' + str(row['编号'])

    # Check for time and permission violations
    for _, account_row in a_table[a_table['账号'] == account].iterrows():
        if account_row['操作表'] == table_name:
            # Time check
            operation_time = datetime.strptime(row['操作时间'], '%Y/%m/%d %H:%M:%S').time()
            start_time, end_time = account_row['可操作时间段(时:分:秒)'].split('~')
            start_time = datetime.strptime(start_time, '%H:%M:%S').time()
            end_time = datetime.strptime(end_time, '%H:%M:%S').time()

            if not (start_time <= operation_time <= end_time):
                return str(account_row['编号']) + '_' + str(row['编号'])  # Time violation

            # Permission check
            permissions = account_row['权限'].split(',')
            if operation_type and operation_type not in permissions:
                return str(account_row['编号']) + '_' + str(row['编号'])  # Permission violation

    return None

def custom_sort(violation):
    parts = violation.split('_')
    return (int(parts[0]), int(parts[1]))

def main():
    a_table_path = '../iris_data/a_table_test.csv'
    b_table_path = '../iris_data/b_table_test.csv'

    a_table = load_csv(a_table_path)
    b_table = load_csv(b_table_path)

    account_table_map = preprocess_a_table(a_table)

    violations = []
    for _, row in b_table.iterrows():
        violation = is_valid_operation(row, a_table, account_table_map)
        if violation:
            violations.append(violation)

    # Sort the violations based on the numerical part after the underscore
    violations_sorted = sorted(violations, key=custom_sort)

    # Join the sorted list into a string
    violations_joined = ','.join(violations_sorted)

    # Calculate MD5 hash
    md5_hash = hashlib.md5(violations_joined.encode()).hexdigest()

    print(f"Sorted Violations: {violations_joined}")
    print(f"MD5 Hash: {md5_hash}")


if __name__ == "__main__":
    main()
相关推荐
七牛云行业应用8 分钟前
重构实录:我删了 5 家大模型 SDK,只留了 OpenAI 标准库
python·系统架构·大模型·aigc·deepseek
知乎的哥廷根数学学派14 分钟前
基于多模态特征融合和可解释性深度学习的工业压缩机异常分类与预测性维护智能诊断(Python)
网络·人工智能·pytorch·python·深度学习·机器学习·分类
亲爱的非洲野猪34 分钟前
Java锁机制八股文
java·开发语言
LawrenceLan40 分钟前
Flutter 零基础入门(十二):枚举(enum)与状态管理的第一步
开发语言·前端·flutter·dart
一人の梅雨1 小时前
亚马逊SP-API商品详情接口轻量化实战:合规与商业价值提取指南
python
charlie1145141912 小时前
从 0 开始的机器学习——NumPy 线性代数部分
开发语言·人工智能·学习·线性代数·算法·机器学习·numpy
catchadmin2 小时前
Laravel12 + Vue3 的免费可商用商业级管理后台 CatchAdmin V5 正式发布
开发语言·php
袁气满满~_~2 小时前
Python数据分析学习
开发语言·笔记·python·学习
axinawang3 小时前
二、信息系统与安全--考点--浙江省高中信息技术学考(Python)
python·浙江省高中信息技术
寻星探路3 小时前
【算法专题】滑动窗口:从“无重复字符”到“字母异位词”的深度剖析
java·开发语言·c++·人工智能·python·算法·ai