Re: joining strings question



patrick.waldo@xxxxxxxxx wrote:
Hi all,

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...

I've racked my brain for a while about this and I can't seem to figure
it out. Any ideas would be much appreciated.

Thanks

data = ['RULES', 'title','subtitle','pdf',
'title1','subtitle1','pdf1','NOTICES','title2','subtitle2','pdf','title3','subtitle3','pdf']
olist = []
while data:
if data[0] == data[0].upper():
olist.append([data[0]])
del data[0]
else:
olist[-1].append(data[0]+' '+data[1])
olist[-1].append(data[2])
del data[:3]
print olist


However, I suspect you should be asking yourself whether this is really an appropriate data structure for your needs. If you say what you are trying to achieve in the large rather than focusing on a limited programming issue there may be much better solutions.

I suspect, for example, that a dict indexed by the categories and with the entries each containing a list of tuples might suit your needs much better, i.e.

{
'RULES': [('title subtitle', 'pdf'),
('title1 subtitle1', 'pdf')],
'NOTICES': [('title2 subtitle2', 'pdf'),
'title3 subtitle3', 'pdf')]}

One final observation: if all the files are PDFs then you might just as well throw the 'pdf' strings away and use a constant extension when you try and open them or whatever :-). Then the lists of tuples i the dict example could just become lists of strings.

regards
Steve

--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

.



Relevant Pages

  • Re: joining strings question
    ... I'm using BeautifulSoup to strip the tables from the ... with the name of the record and the pdf. ... Availability for Lead National Ambient Air Quality Standard Review, ', ... example could just become lists of strings. ...
    (comp.lang.python)
  • Re: PDF files
    ... Actually I have three different lists to list. ... It is not necessary to display the PDF file contents. ... Active Members, Members By Birthday, and Inactive Members. ...
    (microsoft.public.frontpage.programming)
  • Re: Keith - Seeking File Name
    ... not five minutes ago I grabbed a copy of the Perfect20 PDF. ... have a PrC document that runs 219 pages from the Community 3E site. ... You could do CSV lists then organize them as you see fit by ... list creation tool - let me know. ...
    (rec.games.frp.dnd)
  • Re: Presenting information for the public.
    ... He relies on reading equipment to ... read the lists, and PDF is notoriously unreliable for things like ... The list is very easy to understand and in PDF. ...
    (uk.tech.digital-tv)
  • Re: Presenting information for the public.
    ... 'public' will cock it up, totally not realising that its more likely to get ... read the lists, and PDF is notoriously unreliable for things like ...
    (uk.tech.digital-tv)