您将如何在Python中压缩未知数量的列表? [英] How would you zip an unknown number of lists in Python?

查看:49
本文介绍了您将如何在Python中压缩未知数量的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下列表:

assignment = ['Title', 'Project1', 'Project2', 'Project3']
grades = [ ['Jim', 45, 50, 55], \
           ['Joe', 55, 50, 45], \
           ['Pat', 55, 60, 65] ]

我可以使用以下代码压缩列表:

I could zip the lists using the following code:

zip(assignment, grades[0], grades[1], grades[2])

如果成绩列表中包含未知数量的项目,我将如何使用zip函数将其压缩?

How would I use the zip function to zip this if the grades list contains an unkown number of items?

推荐答案

您可以使用*

You can use * to unpack a list into positional parameters:

zip(assignment, *grades)

这篇关于您将如何在Python中压缩未知数量的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆