views:

58

answers:

1

In my script I build a dictionary of keys(albums) mapped to artists(values) so that I can do a quick lookup of what artists made what albums. However, I want the user to be able to find all albums which contain a substring. For example a search on "Light" should return

[Light Chasers] = Cloud Cult and also [Night Light] = Au Revoir Simone

What's the best way to do this? Should I even be using a dictionary?

+4  A: 
[(k, v) for (k, v) in D.iteritems() if 'Light' in k]
Ignacio Vazquez-Abrams
Ah, I was thinking of something like this, I just wouldn't have expressed it so elegantly. I secretly was hoping there might be some sneaky more efficient solution though :)
Sushisource
If performance is critical, memory is cheap, and the record collection changes only rarely, you could build another "index" dict with individual words as keys and list-of-albums-containing-that-word as values. Unless you're doing a lot of lookups and have a truly huge record collection it's probably not worthwhile.
Russell Borogove