真匿名打脸爱好者 2017-12-30
列表:
>>> 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}