Old, very old informatical problem: I want to "print" grouped data with
head information, that is:
eingabe=[
("Stuttgart","70197","Fernsehturm","20"),
("Stuttgart","70197","Brotmuseum","123"),
("Stuttgart","70197","Porsche","123123"),
("Leipzig","01491","Messe","91822"),
("Leipzig","01491","Schabidu","9181231"),
]
shall give: ( Braces are not important...)
'Stuttgart', '70197'
--data-- ('Fernsehturm', '20')
--data-- ('Brotmuseum', '123')
--data-- ('Porsche', '123123')
Leipzig', '01491'
--data-- ('Messe', '91822')
--data-- ('Schabidu', '9181231')
my first approach was:
from itertools import groupby
from operator import itemgetter
for key, bereich in groupby(eingabe,itemgetter(0)):
print "Area:",key
headnotprinted=True
for data in bereich:
if headnotprinted:
headnotprinted=False
print "additional head info", data[1]
print "--data--", data[2:]
leading to:
Area: Stuttgart
additional head info 70197
--data-- ('Fernsehturm', '20')
--data-- ('Brotmuseum', '123')
--data-- ('Porsche', '123123')
Area: Leipzig
additional head info 01491
--data-- ('Messe', '91822')
--data-- ('Schabidu', '9181231')
which is quite what I expected. But ...
if headnotprinted:
headnotprinted=False
print "additional head info", data[1]
REALLY looks patched, not programmed.
my second try was:
def getdoublekey(row):
return row[0:2]
for key, bereich in groupby(eingabe,getdoublekey):
print "Area:",key
for data in bereich:
print "--data--", data[2:]
which indeed leeds to the expected result, while looking less "hacky" ..
on the other hand side, that "getdoublekey" ist not very flexible; when
doing the same with 3 Columns forming the head information, I have to
define the next function...
gettriplekey(row):
return (row[1], row[0], ---yadda yadda yadda
so, what is the best recommended practice for this usual problem within
Python?
Harald