當(dāng)前位置:
首頁 >
12月29日二周五次【Python基础语法】
發(fā)布時(shí)間:2025/3/15
52
豆豆
生活随笔
收集整理的這篇文章主要介紹了
12月29日二周五次【Python基础语法】
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2.8 統(tǒng)計(jì)系統(tǒng)剩余的內(nèi)存
2.9 數(shù)據(jù)類型轉(zhuǎn)換計(jì)算(計(jì)算mac地址)
3.0 數(shù)據(jù)類型轉(zhuǎn)換(列表與字典相互轉(zhuǎn)換)
2.8 統(tǒng)計(jì)系統(tǒng)剩余的內(nèi)存
#!/usr/bin/python # Python2 寫的統(tǒng)計(jì)系統(tǒng)剩余的內(nèi)存 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 數(shù)據(jù)類型轉(zhuǎn)換計(jì)算(計(jì)算mac地址)
- 十六進(jìn)制字符串轉(zhuǎn)為十進(jìn)制 int('12', 16) > 18 int('0x12', 16) > 18
- 十進(jìn)制轉(zhuǎn)為十六進(jìn)制 hex(10) > '0xa' int ('a',16) > 10
- 十進(jìn)制轉(zhuǎn)為字符串(只能是數(shù)字) srt(10) > '10'
- 字符串轉(zhuǎn)為十進(jìn)制(只能是數(shù)字) int('')
- 計(jì)算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 數(shù)據(jù)類型轉(zhuǎn)換(列表與字典相互轉(zhuǎn)換)
- 字符串轉(zhuǎn)列表
- list(srting)
- 列表轉(zhuǎn)字符串
- ".join(list)
- 字符串轉(zhuǎn)元組
- tuple(string)
- 元組轉(zhuǎn)字符串
- ".join(tuple)
- 列表與元組相互轉(zhuǎn)換
- 字典轉(zhuǎn)列表
- 字典的items()方法
- 列表轉(zhuǎn)字典
- dict() s = 'abc' l = ['a','a','b'] t = ('l','p','q') d = {'a':1, 'b':2} print('元組轉(zhuǎn)字符串.') print (str(t)) print('列表轉(zhuǎn)字符串.') print (str(l)) print('字典轉(zhuǎn)字符串.') print (str(d)) print('####################') print('字符串轉(zhuǎn)元組') print (tuple(s)) print('列表轉(zhuǎn)元組') print (tuple(l)) print('字典轉(zhuǎn)元組') print (tuple(d.items())) print('####################') print('字符串轉(zhuǎn)列表') print (list(s)) print('元組轉(zhuǎn)列表') print (list(t)) print('字典轉(zhuǎn)列表') print (list(d.items())) print('####################') tt = (['a',1],['b',2]) ttt = (('a',3),('b',4)) print('元組轉(zhuǎn)字典(元組元素是元組)') print (dict(tt)) print('元組轉(zhuǎn)字典(元組元素是列表)') print (dict(ttt)) print('####################') ll = [['a',5],['b',6]] lll = [('a',7),('b',8)] print('列表轉(zhuǎn)字典(列表元素是列表)') print (dict(ll)) print('列表轉(zhuǎn)字典(列表元素是元組)') print (dict(lll))
轉(zhuǎn)載于:https://blog.51cto.com/13542406/2055966
總結(jié)
以上是生活随笔為你收集整理的12月29日二周五次【Python基础语法】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 防范攻击 加强管控 - 数据库安全的16
- 下一篇: Python面向对象高级编程