Python--迭代

技术 · 2019-01-29 · 44 人浏览

字典迭代

names = {'Mary Wang': 'She is very cute',
         'William Liu': 'He is very handsome',
         'Tom Zhao': 'He is a little bit shy',
         'Jary Qian': 'He is very rich'}
for key, value in names.items():
    print('Name:{}     Saying:{}'.format(key, value))
Name:Mary Wang     Saying:She is very cute
Name:William Liu     Saying:He is very handsome
Name:Tom Zhao     Saying:He is a little bit shy
Name:Jary Qian     Saying:He is very rich

Zip迭代

  • zip

    names = ['William', 'Mary', 'Tommy', 'Jimmy']
    heights = [185, 175, 173, 164]
    print(list(zip(names, heights)))
    for i, name in zip(range(len(names)), names):
      print(i+1, name)
  • William
  • Mary
  • Tommy
  • Jimmy

  • zip迭代器和Enumerate()

    names = ['William', 'Mary', 'Tommy', 'Jimmy']
    for i, name in enumerate(names):
      print(i+1,name)
  • William
  • Mary
  • Tommy
  • Jimmy

    ---
  • zip迭代器zip()反向

    • zip(*)--进行反向
    names = ['William', 'Mary', 'Tommy', 'Jimmy']
    heights = [185, 175, 173, 164]
    names_heights = list(zip(names, heights))
    print(names_heights)
    names_2, heights_2 = zip(*names_heights)
    print(names_2)
    [('William', 185), ('Mary', 175), ('Tommy', 173), ('Jimmy', 164)]
    ('William', 'Mary', 'Tommy', 'Jimmy')

迭代器和生成器

  • 迭代器:iterator

    • 列表是可以迭代的对象但不是迭代器
    • 迭代器是一个数据流,没有生成出来
    • enumerate就是1个迭代器
    list_1 = [1,2,3,4,5,6,7,8,9]
    print(enumerate(list_1))
    <enumerate object at 0x00000245FC4425A0>

    list_1 = [1,2,3,4,5,6,7,8,9]
    print(list(enumerate(list_1)))
    [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]

    开始序号可以更改


  • 生成器:generator

    • 用函数可以创造迭代器(利用yield一次返回一个值)
    def my_range(x):
      a=0
      while a<x:
          yield a
          a+=1
    print(my_range(20))
    print(list(my_range(20)))
    <generator object my_range at 0x000001DE950227C8>
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

  • 自己创建的迭代器

    • 迭代器可以少占用内存
    • 如果list内容过多,内存存储不下需要使用迭代器
  • 自己编写enumerate()

    steps=['Python','Git','Deap','AI']
    def my_enumerate(iterable, star = 0):
      count=star
      for element in iterable:
         yield count,element
          count+=1
    for i,step in my_enumerate(steps,1):
      print('Step{}:{}'.format(i,step))
    Step1:Python
    Step2:Git
    Step3:Deap
    Step4:AI
  • 编写生成器对大数据进行拆分

    def chunker(iterable,size):
      for i in range(0,len(iterable),size):
          yield iterable[i:i+size]
    
    for chunk in chunker(range(100),20):
      print(list(chunk))
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
    [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
    [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
    [80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
python 迭代器 产生器 Zip迭代 字典迭代
Theme Jasmine by Kent Liao