# 示例代码:使用 Python 的 zip 函数
# zip 函数用于将多个可迭代对象中的元素配对,返回一个元组的迭代器。
# 创建两个列表
names = ["Alice", "Bob", "Charlie"]
scores = [95, 85, 90]
# 使用 zip 函数将两个列表配对
paired_data = zip(names, scores)
# 将配对后的结果转换为列表并打印
paired_list = list(paired_data)
print(paired_list) # 输出: [('Alice', 95), ('Bob', 85), ('Charlie', 90)]
# 如果两个可迭代对象的长度不同,zip 会停止于最短的那个可迭代对象结束。
short_names = ["Alice", "Bob"]
long_scores = [95, 85, 90, 88]
paired_data_uneven = zip(short_names, long_scores)
paired_list_uneven = list(paired_data_uneven)
print(paired_list_uneven) # 输出: [('Alice', 95), ('Bob', 85)]
上一篇:python运算符优先级顺序
下一篇:python定义函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站