12月29日二周五次【Python基础语法】
生活随笔
收集整理的這篇文章主要介紹了
12月29日二周五次【Python基础语法】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2.8 統計系統剩余的內存
2.9 數據類型轉換計算(計算mac地址)
3.0 數據類型轉換(列表與字典相互轉換)
2.8 統計系統剩余的內存
#!/usr/bin/python # Python2 寫的統計系統剩余的內存 with open('/proc/meminfo') as f:for ln in f:if ln.startswith('MemTotal'):total = ln.split()[1]continueif ln.startswith('MemFree'):free = ln.split()[1]break print "%.2f" % (int(free)/1024.0)+'M'2.9 數據類型轉換計算(計算mac地址)
- 十六進制字符串轉為十進制 int('12', 16) > 18 int('0x12', 16) > 18
- 十進制轉為十六進制 hex(10) > '0xa' int ('a',16) > 10
- 十進制轉為字符串(只能是數字) srt(10) > '10'
- 字符串轉為十進制(只能是數字) int('')
- 計算mac地址+1 #!/usr/bin/evn python # macaddr = '00:0C:29:D1:6F:E9' prefix_mac = macaddr[:-3] last_tow = macaddr[-2:] if last_tow.upper() == 'FF': last_tow = '00' plus_one = int(last_tow, 16) + 1 if plus_one in range(16): new_last_two = hex(plus_one)[2:] new_last_two = '0' + new_last_two else: new_last_two = hex(plus_one)[2:] new_mac = prefix_mac + ':' + new_last_two
3.0 數據類型轉換(列表與字典相互轉換)
- 字符串轉列表
- list(srting)
- 列表轉字符串
- ".join(list)
- 字符串轉元組
- tuple(string)
- 元組轉字符串
- ".join(tuple)
- 列表與元組相互轉換
- 字典轉列表
- 字典的items()方法
- 列表轉字典
- dict() s = 'abc' l = ['a','a','b'] t = ('l','p','q') d = {'a':1, 'b':2} print('元組轉字符串.') print (str(t)) print('列表轉字符串.') print (str(l)) print('字典轉字符串.') print (str(d)) print('####################') print('字符串轉元組') print (tuple(s)) print('列表轉元組') print (tuple(l)) print('字典轉元組') print (tuple(d.items())) print('####################') print('字符串轉列表') print (list(s)) print('元組轉列表') print (list(t)) print('字典轉列表') print (list(d.items())) print('####################') tt = (['a',1],['b',2]) ttt = (('a',3),('b',4)) print('元組轉字典(元組元素是元組)') print (dict(tt)) print('元組轉字典(元組元素是列表)') print (dict(ttt)) print('####################') ll = [['a',5],['b',6]] lll = [('a',7),('b',8)] print('列表轉字典(列表元素是列表)') print (dict(ll)) print('列表轉字典(列表元素是元組)') print (dict(lll))
轉載于:https://blog.51cto.com/13542406/2055966
總結
以上是生活随笔為你收集整理的12月29日二周五次【Python基础语法】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 防范攻击 加强管控 - 数据库安全的16
- 下一篇: “附近的小程序”可以直接找“餐饮” 非管