gpt4 book ai didi

python - 如何从列表中的多个列表中创建一个表格?

转载 作者:行者123 更新时间:2023-12-01 04:26:44 25 4
gpt4 key购买 nike

如果我们有多个列表并且该列表包含在列表中,我该如何制作表格?

例如,如果我们有

List1= [['apple','ball','cat'],['rat','hat','mat']]

List2=[[1,4,5,6],[2,'rat',5,6]]

List3 = [[23,34,54],[12,23]]

请注意每个列表中的列表数量相同,但列表中的项目数量不同。我正在寻找的结果看起来像这样

List1    List2   List3
apple 1 23
ball 4 34
cat 5 54
rat 6 12
hat 2 23
mat rat
5
6

最佳答案

这应该做你想要的..

from itertools import chain, zip_longest
def lister(l1, l2, l3):
print('List1 List2 List3')
for a,b,c in zip_longest(chain(*l1), chain(*l2), chain(*l3), fillvalue=''):
print('{:7s} {:7s} {:7s}'.format(str(a),str(b),str(c)))

然后你就调用它。

>>> lister(List1, List2, List3)

List1 List2 List3
apple 1 23
ball 4 34
cat 5 54
rat 6 12
hat 2 23
mat rat
5
6

关于python - 如何从列表中的多个列表中创建一个表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33000684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com