Dryden Long
Dryden Long

Reputation: 10190

Matching Items in Lists - Python

So I know how to match items in two lists, but what I am curious about is how to link the two matches. For example, I have a browser based app and am using Jinja2 as my template language.

My program pulls a list of PDF files which also have corresponding XML files. (The files are named similarly, i.e. foo.xml contains the data for foo.pdf) The list of PDF files is displayed on the page, and when the user clicks on the name of a file from the list of PDFs, that file's XML data, if it exists yet, would be displayed in a little pop-up.

So, I guess my question would be, how do I connect the dots and specify the correct xml file to be displayed since col_list[0] is not always going to be the same file?

Below is my code creating the list of pdf files:

 col_list = '<li class="ui-widget-content">'.join('%s</li>' % (os.path.splitext(filename)[0])
                     for filename in listfiles
                     if filename.endswith('.pdf')
                     )

Thanks!

Edit:

I'm going to give a different example in hopes of being less confusing.

List 'A' is an ever-changing list of PDF files (foo.pdf, bar.pdf, etc.)
List 'B' is an ever-changing list of XML files named the same as list 'A' (foo.xml, bar.xml, etc)

I am looping over both lists, and creating variables for each list. If these lists were identical, I could simply call list_b[0] to get the xml data for the first file, which would also be the first PDF. But, since some PDFs do not have XML files yet, the order of the lists do not match up. Let's say list_b[0] is foo.xml and list_a[3] is foo.pdf How can I tell Python that I want the XML data for foo.pdf when the order of the lists is ever-changing?

Sorry for the confusion.

Upvotes: 1

Views: 447

Answers (1)

ᅠᅠᅠ
ᅠᅠᅠ

Reputation: 67040

If I understand correctly: you want to use a set for the XML filenames, and look them up:

pdfs = ['a.pdf', 'b.pdf', 'c.pdf', 'd.pdf']
xmls = ['a.xml', 'd.xml', 'b.xml']

xml_set = set(xmls)

result = []
for pdf in pdfs:
    xml = pdf.replace('.pdf', '.xml')
    if xml in xml_set:
        result.append('Matched %s to %s' % (pdf, xml))
    else:
        result.append("%s doesn't have a corresponding XML file" % (pdf,))

print result

Upvotes: 1

Related Questions