在Python中的列表内压缩列表 [英] Zipping lists within a list in Python

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

问题描述

我有一个列表列表

big_list = [['a1','b1','c1'], ['a2','b2','c3'], ['a3','b3','c3']]

如何在列表中压缩列表?

how do I zip the lists within this list?

我想做的是zip(list1,list2,list3),但是要动态地做

what I want to do is zip(list1,list2,list3), but do this dynamically

我认为它必须与我不熟悉的argskwargs有关,欢迎任何解释

I believe it has to do smth with args and kwargs which I am not familiar with, any explanation is welcome

谢谢

推荐答案

使用*args参数扩展语法:

zip(*big_list)

*(闪屏)告诉Python将每个元素都以可迭代方式接受,并将其作为单独的参数应用于函数.

The * (splash) tells Python to take each element in an iterable and apply it as a separate argument to the function.

演示:

>>> big_list = [['a1','b1','c1'], ['a2','b2','c3'], ['a3','b3','c3']]
>>> zip(*big_list)
[('a1', 'a2', 'a3'), ('b1', 'b2', 'b3'), ('c1', 'c3', 'c3')]

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

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