> I have some data with some categories, titles, subtitles, and a link > to their pdf and I need to join the title and the subtitle for every > file and divide them into their separate groups. > > So the data comes in like this: > > data = ['RULES', 'title','subtitle','pdf', > 'title1','subtitle1','pdf1','NOTICES','title2','subtitle2','pdf','title3','subtitle3','pdf'] > > What I'd like to see is this: > > [RULES', 'title subtitle','pdf', 'title1 subtitle1','pdf1'], > ['NOTICES','title2 subtitle2','pdf','title3 subtitle3','pdf'], etc...
The following iterator yields things that look like each of those items: def category_iterator(source): source = iter(source) last_cat = None entries = [] try: while True: item = source.next() if item == item.upper(): # categories are uppercase if last_cat: yield [last_cat] + entries last_cat = item entries = [] else: title = item subtitle = source.next() link = source.next() entries.append('%s %s' % (title, subtitle)) entries.append(link) except StopIteration: if last_cat: yield [last_cat] + entries if __name__ == '__main__': data = ['RULES', 'title','subtitle','pdf', 'title1','subtitle1','pdf1', 'NOTICES', 'title2','subtitle2','pdf', 'title3','subtitle3','pdf'] for compact_category_info in category_iterator(data): print repr(compact_category_info) If your input data is malformed, you may get peculiar results depending on how pathologically malformed that data is. Hope this helps, -tkc -- http://mail.python.org/mailman/listinfo/python-list