enumerate()

enumerate() 函数,它能将列表元素和该元素的索引“打包”成一个个元组,然后一一列举出来:

# 定义一个列表 lst
name_lst = ['a', 'b', 'c']

# 用 enumerate() 函数枚举列表元素及索引
name_with_index = enumerate(name_lst)

# 用 list() 函数将 enumerate() 函数的结果转换成列表
name_with_index = list(name_with_index)

print(name_with_index)
# 输出:[(0, 'a'), (1, 'b'), (2, 'c')]

你可能感兴趣的:(python,#,python内置函数,python,enumerate)