日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

12月29日二周五次【Python基础语法】

發布時間:2025/3/15 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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基础语法】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。