python学习笔记5—数据类型转换
生活随笔
收集整理的這篇文章主要介紹了
python学习笔记5—数据类型转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據類型轉換
16進制和10進制之間轉換
In?[18]:?int('a',16)????????把16進制的a轉換為10進制的10,16說明a是16進制數 Out[18]:?10 In?[13]:?int('0xa',16)??把16進制的0xa轉換為十進制的10,16說明0xa是16進制數 Out[13]:?10 In?[14]:?int('0xb',16)??把16進制的0xb轉換為十進制的11,16說明0xb是16進制數 Out[14]:?11 In?[16]:?hex(10)?????把10進制的10轉化為十六進制的0xa Out[16]:?'0xa'[root@133?filevalue]#?vim?mac.py#!/usr/bin/python macaddr?=?"A4:BA:DB:20:93:E9"?? prefix_mac?=?macaddr[:-3]??#獲取A4:BA:DB:20:93 last_two?=?macaddr[-2:]????#獲取E9 plus_one?=?int(last_two,?16)?+?1?#將E9轉化為10進制233,然后+1,變為234 new_last_two?=?hex(plus_one)[-2:]???#新的mac最后兩位將234轉換為16進制ea new_mac?=?prefix_mac?+?':'?+?new_last_two?#新的mac地址是?A4:BA:DB:20:93?+?':'?+?ea print?new_mac.upper()??#打印新的mac地址A4:BA:DB:20:93:ea并且轉換為大寫A4:BA:DB:20:93:EA[root@133?filevalue]#?python?mac.py? A4:BA:DB:20:93:EA[root@133?filevalue]#?vim?mac.py?#!/usr/bin/pythonmacaddr?=?"A5:BA:DB:20:93:03" prefix_mac?=?macaddr[:-3] print?prefix_mac last_two?=?macaddr[-2:] print?last_two plus_one?=?int(last_two,?16)?+?1 print?plus_one if?plus_one?in?range(10):new_last_two?=?hex(plus_one)[2:]print?new_last_twonew_last_two?=?'0'?+?new_last_two else:new_last_two?=?hex(plus_one)[2:]print?new_last_twoif?len(new_last_two)?==?1:new_last_two?=?'0'?+?new_last_twoprint?new_last_two new_mac?=?prefix_mac??+?':'?+??new_last_two print?new_mac.upper()[root@133?filevalue]#?python?mac.py? A5:BA:DB:20:93?? 03???最后兩位是03,轉換為10進制是4 4????把4轉換為16進制是4 4????在4前面加上0 A5:BA:DB:20:93:04[root@133?filevalue]#?vim?mac.py?#!/usr/bin/pythonmacaddr?=?"A5:BA:DB:20:93:0a" prefix_mac?=?macaddr[:-3] print?prefix_mac last_two?=?macaddr[-2:] print?last_two plus_one?=?int(last_two,?16)?+?1 print?plus_one if?plus_one?in?range(10):new_last_two?=?hex(plus_one)[2:]print?new_last_twonew_last_two?=?'0'?+?new_last_two else:new_last_two?=?hex(plus_one)[2:]print?new_last_twoif?len(new_last_two)?==?1:new_last_two?=?'0'?+?new_last_twoprint?new_last_two new_mac?=?prefix_mac??+?':'?+??new_last_two print?new_mac.upper()[root@133?filevalue]#?python?mac.py? A5:BA:DB:20:93? 0a??對應10進制的10,加1是11 11??11對應16進制的b b???注意:這里少了0, 0b??加一個0,變成0b,然后用upper變成0B A5:BA:DB:20:93:0B列表和字符串的轉換
字符串轉列表
In?[22]:?s?=?'abc'In?[23]:?list(s)??????#list(s)將字符串s轉換為list列表 Out[23]:?['a',?'b',?'c']In?[24]:?l?=?list(s)??#定義list?lIn?[25]:?l????????????#查看list?l Out[25]:?['a',?'b',?'c']In?[26]:?'.'.join(l)??#使用join的方法將list的l格式化 Out[26]:?'a.b.c'In?[27]:?a?=?'a'??????In?[28]:?help(a.join) Help?on?built-in?function?join:join(...)S.join(iterable)?->?stringReturn?a?string?which?is?the?concatenation?of?the?strings?in?theiterable.??The?separator?between?elements?is?S.?#S是分隔符,這里用的是. (END)?In?[32]:?'?'.join(l)??#用空字符做分隔符 Out[32]:?'a?b?c'字符串轉元祖(與字符串轉列表方法一樣)
In?[33]:?s Out[33]:?'abc'In?[34]:?s?=?'abc'In?[35]:?tuple(s) Out[35]:?('a',?'b',?'c')In?[36]:?t?=?tuple(s)In?[37]:?t Out[37]:?('a',?'b',?'c')In?[38]:?'.'.join(t) Out[38]:?'a.b.c'In?[39]:?'??'.join(t) Out[39]:?'a??b??c'列表和元祖的互相轉換
字典轉換為列表
In?[46]:?dic Out[46]:?{'a':?1,?'b':?2}In?[47]:?dic.items()?????#dic.items()函數將字典轉換為組成的列表,列表元素是元祖,元祖是字典中的key和value的對應關系 Out[47]:?[('a',?1),?('b',?2)]將列表轉換為字典(前提條件是:列表內的元祖是由兩個元素組成的,key和value)
In?[48]:?l1?=?dic.items()In?[49]:?l1 Out[49]:?[('a',?1),?('b',?2)]In?[50]:?dict(l1)??#使用dict(l1)函數將列表轉換為字典 Out[50]:?{'a':?1,?'b':?2}轉載于:https://blog.51cto.com/daixuan/1774533
總結
以上是生活随笔為你收集整理的python学习笔记5—数据类型转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webservice / cxf 开发经
- 下一篇: There was a problem