python 可迭代对象与迭代器之间的转换
列表:
>>> l = [1, 2, 3, 4] >>> l_iter = iter(l) >>> l_iter <list_iterator object at 0x10199e860> >>> l = list(l_iter) >>> l [1, 2, 3, 4]
字符串:
>>> s = 'abcdef' >>> l = list(s) >>> l ['a', 'b', 'c', 'd', 'e', 'f'] >>> i = iter(l) >>> i <list_iterator object at 0x10199e8d0> >>> s = ''.join(l) >>> s 'abcdef' >>> s = ''.join(i) >>> s 'abcdef'
字典:
>>> d = dict(one=1, two=2) >>> d {'one': 1, 'two': 2} >>> i = iter(d) >>> i <dict_keyiterator object at 0x1019a6458> # 键值得迭代器 >>> for k in i: ... print(k) ... one two
元组:
>>> t = (1, 2, 3, 4) >>> t_iter = iter(t) >>> t_iter <tuple_iterator object at 0x10199eb00> >>> t = tuple(t_iter) >>> t (1, 2, 3, 4)
集合:
>>> s = {1, 2, 3, 4} >>> s_iter = iter(s) >>> s_iter <set_iterator object at 0x1019a5d38> >>> s = set(s_iter) >>> s {1, 2, 3, 4}
相关推荐
Andrewjdw 2020-07-26
czsay 2020-06-01
breakpoints 2020-04-20
assastor 2020-03-20
AndroidAmelia 2020-03-01
kikaylee 2020-01-20
fgleeldq 2019-12-22
typhoonpython 2019-12-20
JnX 2020-09-21
joyjoy0 2020-09-18
Jan 2020-08-17
shenxiuwen 2020-08-01
fanhuasijin 2020-06-21
丽丽 2020-06-08
容数据服务集结号 2020-06-08
程序员之怒 2020-04-26
wmsjlihuan 2020-04-26
oDongTianShuiYue 2020-04-26