Python 中的Zip和Enumerate

Python 中的 Zip

  • zip的作用:可以在处理循环时用到,返回一个将多个可迭代对象组合成一个元组序列的迭代器。每个元组都包含所有可迭代对象中该位置的元素。

    my_zip = list(zip(['a', 'b', 'c'], [1, 2, 3])) 
    print(my_zip) # [('a', 1), ('b', 2), ('c', 3)]
  • 正如 range() 一样,我们需要将其转换为列表或使用循环进行遍历以查看其中的元素。

    letters = ['a', 'b', 'c']
    nums = [1, 2, 3]
    
    for letter, num in zip(letters, nums):
        print("{}: {}".format(letter, num))

    输出如下:

    a: 1
    b: 2
    c: 3
  • 除了可以将两个列表组合到一起之外,还可以使用星号拆封列表,返回的是单个元组

    some_list = [('a', 1), ('b', 2), ('c', 3)]
    letters, nums = zip(*some_list)
    print(letters) # ('a', 'b', 'c')
    print(nums) # (1, 2, 3)

Python 中的 Enumerate

  • enumerate 是一个会返回元组迭代器的内置函数,这些元组包含列表的索引和值。当你需要在循环中获取可迭代对象的每个元素及其索引时,将经常用到该函数。

  • 示例代码:

    letters = ['a', 'b', 'c', 'd', 'e']
    for i, letter in enumerate(letters):
        print(i, letter)

    输出如下:

    0 a
    1 b
    2 c
    3 d
    4 e

Python 中的 Zip 和 Enumerate[相关练习]

  • 使用 zip 写一个 for 循环,该循环会创建一个字符串,指定每个点的标签和坐标,并将其附加到列表 points。每个字符串的格式应该为 label: x, y, z。例如,第一个坐标的字符串应该为 F: 23, 677, 4

    解决方案:

    x_coord = [23, 53, 2, -12, 95, 103, 14, -5]
    y_coord = [677, 233, 405, 433, 905, 376, 432, 445]
    z_coord = [4, 16, -6, -42, 3, -6, 23, -1]
    labels = ["F", "J", "A", "Q", "Y", "B", "W", "X"]
    
    points = []
    
    # write your for loop here
    
    for label, x, y, z in zip(labels, x_coord, y_coord, z_coord):
        points.append(label+": " + str(x) + ', ' + str(y) + ', ' + str(z))
    
    for point in points:
        print(point)

    输出如下:

    F: 23, 677, 4
    J: 53, 233, 16
    A: 2, 405, -6
    Q: -12, 433, -42
    Y: 95, 905, 3
    B: 103, 376, -6
    W: 14, 432, 23
    X: -5, 445, -1
  • 使用 zip 创建一个字段 cast,该字典使用 names 作为键,并使用 heights 作为值。

    解决方案:

    cast_names = ["Barney", "Robin", "Ted", "Lily", "Marshall"]
    cast_heights = [72, 68, 72, 66, 76]
    
    cast = dict(zip(cast_names,cast_heights))
    print(cast)

    输出:

    {'Barney': 72, 'Ted': 72, 'Robin': 68, 'Lily': 66, 'Marshall': 76}
  • cast 元组拆封成两个 namesheights 元组。

    解决方案:

    cast = (("Barney", 72), ("Robin", 68), ("Ted", 72), ("Lily", 66), ("Marshall", 76))
    
    # define names and heights here
    
    names,heights = zip(*cast)
    print(names) # ('Barney', 'Robin', 'Ted', 'Lily', 'Marshall')
    print(heights) # (72, 68, 72, 66, 76)
  • 使用 zipdata4x3 矩阵转置成 3x4 矩阵。

    解决方案:

    data = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11))
    
    data_transpose = tuple(zip(*data))
    print(data_transpose) # ((0, 3, 6, 9), (1, 4, 7, 10), (2, 5, 8, 11))
  • 使用 enumerate 修改列表 cast,使每个元素都包含姓名,然后是角色的对应身高。例如,cast 的第一个元素应该从 “Barney Stinson” 更改为 “Barney Stinson 72”。

    解决方案:

    cast = ["Barney Stinson", "Robin Scherbatsky", "Ted Mosby", "Lily Aldrin", "Marshall Eriksen"]
    heights = [72, 68, 72, 66, 76]
    
    for i, c in enumerate(cast):
        cast[i] += ' ' + str(heights[i])
    print(cast) # ['Barney Stinson 72', 'Robin Scherbatsky 68', 'Ted Mosby 72', 'Lily Aldrin 66', 'Marshall Eriksen 76']

猜你喜欢

转载自blog.csdn.net/tyro_java/article/details/80725682