Files
2025PY/day22/04-和字典配合的函数.py
2025-05-22 16:50:44 +08:00

21 lines
698 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 1、enumerate
# 枚举,根据索引号 和 容器 中的值,一个一个拿出来配对组成元组,放入迭代器中,然后返回迭代器。
# 参数
# iterable可迭代数据
# start可以选择开始的索引号默认从0开始索引
list=['apple','banana','cherry']
newlist=[val for val in enumerate(list)]
print(newlist) #[(0, 'apple'), (1, 'banana'), (2, 'cherry')]
# 2、zip
# 将多个中的值,一个个拿出来配对组成元组放入迭代器中,如果某个元素多出,没有匹配项就会被舍弃。
# 参数
# iterable可迭代数据
list1=[1,2,3,4,5]
list2=['a','b','c','d']
list3=['A','B','C']
print([val for val in zip(list1,list2,list3)])