| from datasets import load_dataset | |
| import json | |
| def save_to_json(data, filename): | |
| """保存数据到 JSON 文件""" | |
| with open(filename, 'w', encoding='utf-8') as f: | |
| json.dump(data, f, ensure_ascii=False, indent=4) | |
| print(f"Saved to {filename}, data length: {len(data)}") | |
| # 定义文件路径 | |
| file_path = "/opt/aps/workdir/sunshuang/deep_search/math_data/math_qwq_4524_add_prompt_token_4524.json" | |
| # 加载数据集 | |
| dataset = load_dataset('json', data_files=file_path)['train'] | |
| # 按 seq_token_len 字段升序排序 | |
| sorted_dataset = dataset.sort("seq_token_len") | |
| # 选择最小的 871 条数据 | |
| selected_dataset = sorted_dataset.select(range(871)) | |
| # 获取最后一条数据的 seq_token_len 值 | |
| last_seq_token_len = selected_dataset[-1]['seq_token_len'] | |
| # 输出结果 | |
| print(f"The seq_token_len of the last selected data is: {last_seq_token_len}") | |
| # 保存筛选后的数据集为 JSON 文件 | |
| output_file_path = "/opt/aps/workdir/sunshuang/deep_search/math_data/selected_data_871.json" | |
| # 将筛选得到的数据保存为json | |
| selected_dataset = selected_dataset.to_list() | |
| # 保存为json | |
| save_to_json(selected_dataset, output_file_path) | |
| print(f"Selected data has been saved to {output_file_path}") |