python 按照cpu的使用率对top中的进程排序(排序表格)
来源:互联网 发布:诗词翻译赏析软件 编辑:程序博客网 时间:2024/06/06 13:15
问题:解决如何在python中对表格(二维)进行排序?例如,按照cpu或mem的使用率对top中的进程排序
解决方法:
1. 使用numpy或者panda中的方法对二维矩阵进行操作
2. 使用二维数组(list)存储表格,然后按照某一列排序
下面实现方法二
假设top信息存在于与脚本同目录的txt文件中。具体信息如下:
step1:将文本内容读入list存放
#!/usr/bin/env python3# -*- coding: utf-8 -*-lines = []#read txt file line by line and stored in listwith open('top_info.txt', 'r') as f: lines = f.readlines()print(len(lines))print("initialize arrays:")lines_len = len(lines) - 7 #info need to processcols_name = [i for i in lines[6].split()]arrays = [[0 for i in lines[6].split()] for i in range(lines_len)] #initialize matrixprint(cols_name)print(len(arrays))print(*arrays, sep='\n') #print element in multi lines
运行结果:
step2 : 将要处理的几行信息单独存储
#gets into needs to processfor i in range(7, len(lines)): arrays[i-7] = lines[i].split()print(arrays[0])print(arrays[1])print(arrays[2])
运行结果:
step3 : 按照cpu和mem的使用率排序,降序
关键代码:
from operator import itemgetterprint("sort info:")#cpu_row = [row[8] for row in arrays]arrays_sorted_by_cpu = sorted(arrays, key=itemgetter(8), reverse = True)print(*arrays_sorted_by_cpu, sep='\n')
运行结果:
完整代码:
#!/usr/bin/env python3# -*- coding: utf-8 -*-from operator import itemgetterlines = []#read txt file line by line and stored in listwith open('top_info.txt', 'r') as f: lines = f.readlines()print("the top info in doc is: \n")print(*lines, sep='\n')lines_len = len(lines) - 7 #info need to processarrays = [[0 for i in lines[6].split()] for i in range(lines_len)]#gets into needs to processfor i in range(7, len(lines)): arrays[i-7] = lines[i].split()print("sort info:")arrays_sorted_by_cpu = sorted(arrays, key=itemgetter(8), reverse = True)print(*arrays_sorted_by_cpu, sep='\n')
运行结果:
推荐阅读:
知乎:python二维数组按照某一列进行筛选统计?
http://stackoverflow.com/questions/32400639/python-readline
http://stackoverflow.com/questions/5212870/sorting-a-python-list-by-two-criteria
http://stackoverflow.com/questions/4233476/sort-a-list-by-multiple-attributes
http://stackoverflow.com/questions/20183069/how-to-sort-multidimensional-array-by-column
http://stackoverflow.com/questions/2173797/how-to-sort-2d-array-by-row-in-python
http://stackoverflow.com/questions/8609737/python-comparing-two-massive-sets-of-data-in-the-most-efficient-method-possible
http://stackoverflow.com/questions/4183506/python-list-sort-in-descending-order
http://stackoverflow.com/questions/16326529/python-get-process-names-cpu-mem-usage-and-peak-mem-usage-in-windows
- python 按照cpu的使用率对top中的进程排序(排序表格)
- ruby 按照cpu的使用率对top中的进程排序(排序表格)
- 统计某个进程的cpu使用率使用top命令
- 按照拼音对数组中的中文字符串排序的算法
- python 按照list中的dic的某key排序
- python 计算windows上的某个进程的cpu使用率
- python获取全部进程的CPU、内存使用率
- 转载:python获取全部进程的CPU、内存使用率
- js对表格的排序
- linux使用top命令跟踪进程的CPU占用率、内存使用率
- python字典排序、按照list中的字典的某个key排序
- python dict 按照value排序的试验
- python 按照字典的值进行排序
- 【python】numpy数组中按照特征值对特征向量进行排序
- java 对 汉字排序(按照拼音字母排序)
- python top n排序
- oracle中对排序的总结(按照拼音、部首、笔画排序功能 )
- 对list集合中的对象按照字段属性排序
- 一个炫酷的SearchView搜索动画库
- 关于包络概念的理解
- vector使用方法
- Android_ListView
- Android WiFi调式工具WiFi ADB的使用
- python 按照cpu的使用率对top中的进程排序(排序表格)
- UFLDL 笔记 01 神经网络
- Linux 软件包管理工具 Snapcraft
- Android的SDK更新问题
- iOS中UIWebView的使用详解
- sizeof 是一个关键字
- Android中的对话框
- Hadoop家族环境搭建各种版本选择
- HBase单机安装