python之计算系统空闲内存、列表字典相互转换
来源:互联网 发布:aide表白源码 编辑:程序博客网 时间:2024/05/17 08:46
python之计算系统空闲内存
#!/usr/bin/env python
# -*- coding:utf8 -*-
# @Time : 2017/11/30 14:25
# @Author : hantong
# @File : count_free_memory.py
#统计linux系统空闲内存和所有内存
with open('/proc/meminfo') as fd:
for line in fd:
if line.startswith('MemTotal'):
#startswith是以。。。开头的字符串,上面表示以MemTotal开头的字符串
total = line.split()[1]
#split切片,上面表示下标1的字符串
continue
if line.startswith('MemFree'):
free = line.split()[1]
break
TotalMem = int(total)/1024000.0
#此处除以1024000,换算下来是GB
FreeMem = int(free)/1024000.0
print('Free Memory = '+"%.2f" % FreeMem +'G')
#%.2f表示保留两位小数
print('Total Memory = '+"%.2f" % TotalMem +'G')
执行结果:
Free Memory = 2.20G
Total Memory = 3.83G
字典与列表相互转换
#!/usr/bin/env python# -*- coding:utf8 -*-# @Time : 2017/12/1 13:52# @Author : hantong# @File : fffffff.py#dict与list相互转换dict = {'name':"hanson",'age':"31"}print(list(dict))#把字典转换为列表print(tuple(dict))#把字典转换为元祖# l = tuple(dict)# print(str(l))# print(type(dict))#第二种字典转换为list的转换方式f = dict.items()print(f)print(type(f))#可以看到f的类型是list,此时dict已经被转换为list#list转换为dictnew_dict= {}for i in f: new_dict[i[0]] = i[1]#此处是给字典赋值,左边为key,右边是value print(new_dict) print(type(new_dict))#类型为dict执行结果:
['age', 'name']
('age', 'name')
[('age', '31'), ('name', 'hanson')]
<type 'list'>
{'age': '31'}
<type 'dict'>
{'age': '31', 'name': 'hanson'}
<type 'dict'>
- python之计算系统空闲内存、列表字典相互转换
- python字符串、列表、字典相互转换
- python 字符串,列表,元组,字典相互转换
- Python -- 字典转换为列表
- Python 列表转换为字典
- Python列表、元组、集合、字典的区别和相互转换
- Python列表、元组、集合、字典的区别和相互转换
- Linux 空闲内存和空闲CPU计算
- python基础数据结构——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- python基础学习——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- python 列表 字典 string 互相转换
- Python中列表转换成字典
- Python中元组、列表、字典的遍历和相互转化
- python 字符串 元组 列表 字典 间的相互转化
- Python中元组、列表、字典的遍历和相互转化
- python 字典列表 列表字典
- python json字符串与字典对象相互转换
- python获取系统空闲时间段
- 第三条:尽可能使用const
- 论文Recent Trends in Deep Learning Based Natural Language Processing
- ARM硬件支持java技术Jazelle DBX
- springboot实战之文章汇总
- Sublime Text 3 前端必备插件
- python之计算系统空闲内存、列表字典相互转换
- 怎么把seo技术玩好?
- assertEquals方法
- nodejs express 常用模块之config-lite
- php简单开启gzip压缩方法(zlib.output_compression)
- 通过类名直接获取某个spring对象
- C# 使用资源文件 Resource.resx 的方法
- LeetCode--Reverse Linked List 反转链表(Python)
- linux shell审计--snoopy的注意事项