简体   繁体   English

Python将特定时间附加到列表

[英]Python append specific times to list

I am trying to append 5 times to a list and calculate the average of the values. 我正在尝试将5次附加到列表中并计算值的平均值。 Since the top command is looping forever, I am not able to do so. 由于top命令将永远循环,因此我无法这样做。

#/usr/bin/python
import subprocess

values= []
cpu=subprocess.call(["top -b | awk '/sshd/ {print  $9}'"], shell=True)
values.append(cpu)
print values

Output: 输出:

3.0
0.3
2.3
2.2

First, You need to use subprocess.check_output if you want cpu to have output of the command. 首先,如果您希望cpu具有命令的输出,则需要使用subprocess.check_output .call does not return output of the command. .call不返回命令的输出。

Second you can specify -n 5 in you top command. 其次,您可以在top命令中指定-n 5

#/usr/bin/python
import subprocess

cpu = subprocess.check_output(["top -n 5 -b | awk '/sshd/ {print  $9}'"], shell=True)
values = cpu.split()
print values

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:将项目追加到列表 N 次 - Python: Append item to list N times 将字符串追加到字典列表中的特定值-Python - Append string to specific values in a list of dictionary - Python python:仅附加列表中的特定元素 - python: append only specific elements from a list Python 列表限制特定元素到N次 - Python list limit specific elements to N times 如何在 Python 中使用出现 2 倍的元素填充(附加)列表 - How to populate (append) list in Python with elements, that appear multiple of 2 times 生成数组的最后一个 num 并附加到列表中的次数 python - Generate last num of array and append to list for num of times python 有没有办法在python中的特定索引处附加/扩展带有另一个列表的列表? - Is there a way to append/extend a list with another list at a specific index in python? Python:Append 值到新列表,如果它以特定字母开头,然后 go 到下一个列表和 Z95119AZD4D4 值 - Python: Append value to new list if it starts with an specific letter and then go to next list and append those values 如何将字符附加到列表中项目的特定部分 - Python - How to append character to specific part of item in list - Python Python 嵌套列表:如何将特定元素和 append 打印到每个子列表 - Python Nested List: How to print specific elements and append to each sublist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM