python: avoiding zip truncation of list

后端 未结 1 960
醉话见心
醉话见心 2020-12-20 14:58

I have the following python code that uses zip() and it seems to cause unintended data truncation.

inc_data = [[u\'Period Ending\', u\'Dec 31, 2012\', u\'Dec         


        
相关标签:
1条回答
  • 2020-12-20 15:33

    Use izip_longest:

    from itertools import izip_longest
    
    inc_data = [[u'Period Ending', u'Dec 31, 2012', u'Dec 31, 2011', u'Dec 31, 2010'],
                [u'Total Revenue\n', u'104,507,100\n', u'106,916,100\n', u'99,870,100\n'],
                [u'Cost of Revenue\n',u'56,000,000\n']
                ]
    
    print list(izip_longest(*inc_data, fillvalue=u'')) 
    
    
    # [(u'Period Ending', u'Total Revenue\n', u'Cost of Revenue\n'), 
       (u'Dec 31, 2012', u'104,507,100\n', u'56,000,000\n'), 
       (u'Dec 31, 2011', u'106,916,100\n', u''), 
       (u'Dec 31, 2010', u'99,870,100\n', u'')]
    
    0 讨论(0)
提交回复
热议问题