这篇文章主要介绍了常用的python数据类型转换函数,并用实际例子说明了这些函数的用法:
下面以实例来讲解:
1.int(x,[base])
int()函数把x转换为整数,不会四舍五入。
-
In [1]: int(5.4)
-
Out[1]: 5
-
-
In [2]: int(5.6)
-
Out[2]: 5
-
In [3]: int(3L)
-
Out[3]: 3
-
In [4]: int("7")
-
Out[4]: 7
-
In [5]: int("14",10)
-
Out[5]: 14
-
In [6]: int("14",12)
-
Out[6]: 16 #14+2
2.long(x)
long()函数把x转换为长整数,转换完成后,在数字后面加'L'
-
In [3]: long(24)
-
Out[3]: 24L
-
-
In [4]: long(200.56)
-
Out[4]: 200L
-
In [5]: long('123')
-
Out[5]: 123L
3.float(x)
float()函数把x转换为浮点数
-
In [7]: float(2)
-
Out[7]: 2.0
-
In [8]: float(12L)
-
Out[8]: 12.0
-
In [9]: float("12")
-
Out[9]: 12.0
-
In [10]: float(2.9)
-
Out[10]: 2.8999999999999999
-
4.str(x)
str()函数把x转换为字符串
-
In [12]: a=str(79)
-
-
In [13]: type(a)
-
Out[13]: str
-
-
In [14]: a
-
Out[14]: '79'
5.repr(x)
将对象x转换为表达式字符串
-
In [35]: repr([0,1,2,3])
-
Out[35]: '[0, 1, 2, 3]'
-
-
In [36]: repr('Hello')
-
Out[36]: "'Hello'"
6.tuple(s)
tuple()函数把序列对象转换成tuple。
-
In [37]: tuple('hello world')
-
Out[37]: ('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
-
-
In [38]: tuple([4,5,6,1,2,3])
-
Out[38]: (4, 5, 6, 1, 2, 3)
7.list(s)
list()函数可将序列对象转换成列表。
-
In [39]: list('hello world')
-
Out[39]: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
-
-
In [40]: list((1,2,3,4,5))
-
Out[40]: [1, 2, 3, 4, 5]
8.set(s)
转换为可变集合
-
In [46]: set(['hello','world'])
-
Out[46]: {'hello', 'world'}
-
-
In [47]: set('nihao')
-
Out[47]: {'a', 'h', 'i', 'n', 'o'}
9.frozenset(s)
转换为不可变集合,集合中数据不能更新
-
In [50]: frozenset('this is a book')
-
Out[50]: frozenset({' ', 'a', 'b', 'h', 'i', 'k', 'o', 's', 't'})
10.chr(x)
chr()函数返回ASCII码对应的字符串.
-
In [51]: print chr(65)
-
A
-
-
In [52]: print chr(65)+chr(97)
-
Aa
-
-
In [53]: print chr(23)
-
口
-
-
In [54]: print chr(55)
-
7
11.unichr(x)
将一个整数,转换为Unicode字符
-
In [55]: unichr(55)
-
Out[55]: u'7'
-
-
In [56]: unichr(123)
-
Out[56]: u'{'
-
-
In [57]: unichr(99)
-
Out[57]: u'c'
-
-
In [58]: unichr(68)
-
Out[58]: u'D'
12.ord(x)
ord()函数返回一个字符串参数的ASCII码或Unicode值。
-
In [60]: ord('a')
-
Out[60]: 97
-
-
In [61]: ord(u'A')
-
Out[61]: 65
13.
hex(x)
hex()函数可把整数转换成十六进制数。
-
In [62]: hex(18)
-
Out[62]: '0x12'
-
-
In [63]: hex(121)
-
Out[63]: '0x79'
14.
oct(x)
oct()函数可把给出的整数转换成八进制数。
-
In [64]: oct(8)
-
Out[64]: '010'
-
-
In [65]: oct(121)
-
Out[65]: '0171'
15.其他数据类型之间相互转换
-
In [76]: #Float转换为Long
-
-
In [77]: long(10.8)
-
Out[77]: 10L
-
-
In [78]: #Float转换为Int
-
-
In [79]: int(10.9)
-
Out[79]: 10
-
-
In [80]: #String转换为Int
-
-
In [82]: int('0xa',16)
-
Out[82]: 10
-
-
In [83]: int('1010',2)
-
Out[83]: 10
-
-
In [84]: #String转换为Long
-
-
In [85]: long('0xa',16)
-
Out[85]: 10L
-
-
In [86]: long('101010',2)
-
Out[86]: 42L
-
-
In [87]: #String转换为List
-
-
In [88]: name = 'nihao'
-
-
In [89]: list(name)
-
Out[89]: ['n', 'i', 'h', 'a', 'o']
-
-
In [90]: #Tuple转换为List
-
-
In [91]: atupl = ('this','is','a','book')
-
-
In [92]: list(atupl)
-
Out[92]: ['this', 'is', 'a', 'book']
-
-
In [93]: #String转换为Tuple
-
-
In [94]: str = "hello world"
-
-
In [95]: tuple(str)
-
Out[95]: ('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
-
-
In [96]: #List转换为Tuple
-
-
In [97]: aa = ['nihao','zhongguo','shanghai']
-
-
In [98]: tuple(aa)
-
Out[98]: ('nihao', 'zhongguo', 'shanghai')
-
-
In [99]: #将List和Tuple复合数据类型转换为Dictionary
-
-
In [100]: atupl = ('this','is','a','book')
-
-
In [101]: aa = ['nihao','zhongguo','shanghai']
-
-
In [102]: zip(aa,atupl)
-
Out[102]: [('nihao', 'this'), ('zhongguo', 'is'), ('shanghai', 'a')]
-
-
In [103]: dict(zip(aa,atupl))
-
Out[103]: {'nihao': 'this', 'shanghai': 'a', 'zhongguo': 'is'}
-
-
In [104]: #Dictionary转换为List
-
-
In [105]: dic = {'name':'tom','age':26}
-
-
In [106]: dic.items()
-
Out[106]: [('age', 26), ('name', 'tom')]
阅读(1766) | 评论(0) | 转发(0) |