脚本如何筛选及格不及格数据

wen 实用脚本 31

本文目录导读:

脚本如何筛选及格不及格数据

  1. Python (最常用)
  2. Excel VBA
  3. SQL查询
  4. JavaScript
  5. Shell脚本 (Linux)
  6. PowerShell
  7. 实用的带统计功能版本 (Python)

我来介绍几种常用脚本筛选及格/不及格数据的方法:

Python (最常用)

# 基础筛选
scores = [85, 45, 92, 58, 73, 60, 49, 88]
# 及格分数 >= 60
pass_scores = [s for s in scores if s >= 60]
fail_scores = [s for s in scores if s < 60]
print(f"及格: {pass_scores}")
print(f"不及格: {fail_scores}")
# 使用字典数据
students = [
    {"name": "张三", "score": 85},
    {"name": "李四", "score": 45},
    {"name": "王五", "score": 92}
]
pass_students = [s for s in students if s["score"] >= 60]
fail_students = [s for s in students if s["score"] < 60]

Excel VBA

Sub FilterPassFail()
    Dim lastRow As Long
    Dim i As Long
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    ' 从第2行开始(假设第1行是标题)
    For i = 2 To lastRow
        If Cells(i, 2).Value >= 60 Then  ' 假设分数在B列
            Cells(i, 3).Value = "及格"    ' 结果写入C列
        Else
            Cells(i, 3).Value = "不及格"
        End If
    Next i
End Sub

SQL查询

-- 查询及格学生
SELECT name, score
FROM students
WHERE score >= 60;
-- 查询不及格学生
SELECT name, score
FROM students
WHERE score < 60;
-- 分组统计
SELECT 
    CASE 
        WHEN score >= 60 THEN '及格'
        ELSE '不及格'
    END AS status,
    COUNT(*) AS count,
    GROUP_CONCAT(name) AS students
FROM students
GROUP BY status;

JavaScript

const scores = [85, 45, 92, 58, 73, 60, 49, 88];
// 使用 filter
const passScores = scores.filter(score => score >= 60);
const failScores = scores.filter(score => score < 60);
// 对象数组
const students = [
    {name: "张三", score: 85},
    {name: "李四", score: 45},
    {name: "王五", score: 92}
];
const passStudents = students.filter(s => s.score >= 60);
const failStudents = students.filter(s => s.score < 60);

Shell脚本 (Linux)

#!/bin/bash
# 假设scores.txt每行一个分数
while read score; do
    if [ "$score" -ge 60 ] 2>/dev/null; then
        echo "$score - 及格"
    else
        echo "$score - 不及格"
    fi
done < scores.txt
# 或者使用awk
awk '{if($1 >= 60) print $0" - 及格"; else print $0" - 不及格"}' scores.txt

PowerShell

$scores = @(85, 45, 92, 58, 73, 60, 49, 88)
# 筛选及格
$passScores = $scores | Where-Object {$_ -ge 60}
$failScores = $scores | Where-Object {$_ -lt 60}
# 输出结果
Write-Host "及格:$($passScores -join ', ')"
Write-Host "不及格:$($failScores -join ', ')"

实用的带统计功能版本 (Python)

def analyze_scores(scores, pass_mark=60):
    """分析成绩数据"""
    pass_list = [s for s in scores if s >= pass_mark]
    fail_list = [s for s in scores if s < pass_mark]
    print(f"总分学生数: {len(scores)}")
    print(f"及格人数: {len(pass_list)} ({len(pass_list)/len(scores)*100:.1f}%)")
    print(f"不及格人数: {len(fail_list)} ({len(fail_list)/len(scores)*100:.1f}%)")
    if pass_list:
        print(f"及格最高分: {max(pass_list)}")
        print(f"及格最低分: {min(pass_list)}")
    if fail_list:
        print(f"不及格最高分: {max(fail_list)}")
        print(f"不及格最低分: {min(fail_list)}")
    return pass_list, fail_list
# 使用示例
scores = [85, 45, 92, 58, 73, 60, 49, 88, 95, 32]
pass_scores, fail_scores = analyze_scores(scores)

你需要处理的数据在什么环境中?告诉我具体的场景,我可以给出更针对性的解决方案。

抱歉,评论功能暂时关闭!