python: avoiding zip truncation of list
Question:
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 31, 2011', u'Dec 31, 2010'],
[u'Total Revenuen', u'104,507,100n', u'106,916,100n', u'99,870,100n'],
[u'Cost of Revenuen',u'56,000,000n']
]
inc_data2 = zip(*inc_data)
for i in inc_data2:
print i
It only prints:
(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen')
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n')
But I want it to print the following, but apparently I have to add in fillers u''
by hand in order to prevent zip() from truncating the inc_data. But I don’t know how to code that.
(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen')
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n')
(u'Dec 31, 2011', u'106,916,100n', u'')
(u'Dec 31, 2010', u'99,870,100n', u'')
To describe inc_data above,
inc_data = [ [x],
[y],
[z] ]
How do I make x, y and z to be the same length? And the length is the max length of x, y, or z?
(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen')
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n')
(u'Dec 31, 2011', u'106,916,100n', u'')
(u'Dec 31, 2010', u'99,870,100n', u'')
Sorry for the lengthy and wordy explanation of the problem. Could you help me or point me to a similar question that has been answered, if one exists? many thanks!
Answers:
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 Revenuen', u'104,507,100n', u'106,916,100n', u'99,870,100n'],
[u'Cost of Revenuen',u'56,000,000n']
]
print list(izip_longest(*inc_data, fillvalue=u''))
# [(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen'),
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n'),
(u'Dec 31, 2011', u'106,916,100n', u''),
(u'Dec 31, 2010', u'99,870,100n', u'')]
Python 2.x:
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 Revenuen', u'104,507,100n', u'106,916,100n', u'99,870,100n'],
[u'Cost of Revenuen',u'56,000,000n']
]
print list(izip_longest(*inc_data, fillvalue=u''))
# [(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen'),
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n'),
(u'Dec 31, 2011', u'106,916,100n', u''),
(u'Dec 31, 2010', u'99,870,100n', u'')]
Python 3.xx:
izip_longest() has been renamed to zip_longest()
Use zip_longest:
from itertools import zip_longest
inc_data = [[u'Period Ending', u'Dec 31, 2012', u'Dec 31, 2011', u'Dec 31, 2010'],
[u'Total Revenuen', u'104,507,100n', u'106,916,100n', u'99,870,100n'],
[u'Cost of Revenuen',u'56,000,000n']
]
print list(zip_longest(*inc_data, fillvalue=u''))
# [(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen'),
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n'),
(u'Dec 31, 2011', u'106,916,100n', u''),
(u'Dec 31, 2010', u'99,870,100n', u'')]
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 31, 2011', u'Dec 31, 2010'],
[u'Total Revenuen', u'104,507,100n', u'106,916,100n', u'99,870,100n'],
[u'Cost of Revenuen',u'56,000,000n']
]
inc_data2 = zip(*inc_data)
for i in inc_data2:
print i
It only prints:
(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen')
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n')
But I want it to print the following, but apparently I have to add in fillers u''
by hand in order to prevent zip() from truncating the inc_data. But I don’t know how to code that.
(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen')
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n')
(u'Dec 31, 2011', u'106,916,100n', u'')
(u'Dec 31, 2010', u'99,870,100n', u'')
To describe inc_data above,
inc_data = [ [x],
[y],
[z] ]
How do I make x, y and z to be the same length? And the length is the max length of x, y, or z?
(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen')
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n')
(u'Dec 31, 2011', u'106,916,100n', u'')
(u'Dec 31, 2010', u'99,870,100n', u'')
Sorry for the lengthy and wordy explanation of the problem. Could you help me or point me to a similar question that has been answered, if one exists? many thanks!
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 Revenuen', u'104,507,100n', u'106,916,100n', u'99,870,100n'],
[u'Cost of Revenuen',u'56,000,000n']
]
print list(izip_longest(*inc_data, fillvalue=u''))
# [(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen'),
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n'),
(u'Dec 31, 2011', u'106,916,100n', u''),
(u'Dec 31, 2010', u'99,870,100n', u'')]
Python 2.x:
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 Revenuen', u'104,507,100n', u'106,916,100n', u'99,870,100n'],
[u'Cost of Revenuen',u'56,000,000n']
]
print list(izip_longest(*inc_data, fillvalue=u''))
# [(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen'),
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n'),
(u'Dec 31, 2011', u'106,916,100n', u''),
(u'Dec 31, 2010', u'99,870,100n', u'')]
Python 3.xx:
izip_longest() has been renamed to zip_longest()
Use zip_longest:
from itertools import zip_longest
inc_data = [[u'Period Ending', u'Dec 31, 2012', u'Dec 31, 2011', u'Dec 31, 2010'],
[u'Total Revenuen', u'104,507,100n', u'106,916,100n', u'99,870,100n'],
[u'Cost of Revenuen',u'56,000,000n']
]
print list(zip_longest(*inc_data, fillvalue=u''))
# [(u'Period Ending', u'Total Revenuen', u'Cost of Revenuen'),
(u'Dec 31, 2012', u'104,507,100n', u'56,000,000n'),
(u'Dec 31, 2011', u'106,916,100n', u''),
(u'Dec 31, 2010', u'99,870,100n', u'')]