[Tutor] Searching a text file's contents and comparing them toalist

Nick Raptis airscorp at otenet.gr
Thu Jul 15 01:26:24 CEST 2010


On 07/14/2010 11:57 PM, Eric Hamiter wrote:
> Last question (for today, at least): Right now, the output is less
> than aesthetically pleasing:
>
> (['Located on aisle 1: ', 'bread', 'magazines'], ['Located on aisle 2:
> ', 'juice', 'ice cream'], ['Located on aisle 3: ', 'asparagus'], ['Not
> found in the database: ', 'butter', 'soap'])
>
> How can I format it so it looks more like this:
>
> Located on aisle 1:
> bread
> magazines
>
> Located on aisle 2
> [etc...]
>
>
> I tried putting "\n" into it but it just prints the literal string.
> I'm sure it has to do with the list format of holding multiple items,
> but so far haven't found a way to break them apart.
>
>
>    
Readup on str.join() 
http://docs.python.org/library/stdtypes.html#str.join (link is not that 
helpful I admit, google some uses too)
For example,
print "\n".join(first_run)
will get you started.

I think the end code you're looking for is something like

output = ["\n".join(run) for run in sorted_list]
print "\n".join(output)

but I'm not sure if you've got the hang of list comprehensions by now.

Nick


More information about the Tutor mailing list