0

I've got a dictionary with a structure like this:

{
    'www.site.sx/file1.html' : 'TITLE1 101 Project Caspian', 
    'www.site.sx/file2.html' : 'TITLE2 102 Filter Spencer'
}

I want to be able to have the user just type in title2 102 to match the second entry here so the program can get resources from the associated link. How can I search my dictionary with a wildcard match using the user's inputted value?


Example:

The user types in

title1 101

or perhaps

Title1 101

Now, how can I use this / parse this to get a match in the dictionary?


n.b. - Case sensitivity matters...

user3404787
  • 11
  • 1
  • 6

5 Answers5

0

You might be better off restructuring your data, if possible:

DATA = {
    'title1 101': ('www.site.sx/file1.html', 'Project Caspian'), 
    'title2 102': ('www.site.sx/file2.html', 'Filter Spencer'),
}

Then it's a simple case of doing:

user_input = 'Title1 101'
link, title = DATA[user_input]
Matthew Trevor
  • 14,354
  • 6
  • 37
  • 50
0

Just use in to check if a substring is present in your full text:

[k for k in mydict if userInput.lower() in mydict[k].lower()]

This returns all the url's which have the matching text (certainly, there can be several of them).

sashkello
  • 17,306
  • 24
  • 81
  • 109
0

Given:

In [41]: d = {
   ....: 'www.site.sx/file1.html' : 'title1 101 Project Caspian',
   ....: 'www.site.sx/file2.html' : 'title2 102 Filter Spencer'
   ....: }

In [43]: d.values()
Out[43]: ['title1 101 Project Caspian', 'title2 102 Filter Spencer']

In [48]: d.keys()
Out[48]: ['www.site.sx/file1.html', 'www.site.sx/file2.html']

You can create a function

In [45]: def index1(vals, item):
   ....:     for i, v in enumerate(vals):
   ....:         if item in v: return i
   ....:     return -1

such that

In [46]: index1(d.values(), 'temp')
Out[46]: -1

In [47]: index1(d.values(), 'title2')
Out[47]: 1

Then, you can always do:

In [50]: d.keys()[index1(d.values(), 'title2')]
Out[50]: 'www.site.sx/file2.html'
ssm
  • 5,277
  • 1
  • 24
  • 42
0

I am interpreting your statement about case sensitivity as you want it to work even if the user does not type in the correct case.

One possible solution, which runs in O(log(n)) but has an O(n log n) preprocessing step, is to put the keys into a sorted list and do a binary search to find the correct key, followed by the key lookup.

Your dictionary would look like this:

{
    'TITLE1 101 Project Caspian' : 'www.site.sx/file1.html', 
    'TITLE2 102 Filter Spencer' : 'www.site.sx/file2.html',
}

You would then have a sorted list like this:

['TITLE1 101 Project Caspian', 'TITLE2 102 Filter Spencer']

As a preprocessing step, you would make everything lowercase, to deal with the case issue. When an input x came in from the user, you would

  1. Make it lowercase.
  2. Do a binary search through your list.
  3. Check if the query is a prefix of the element that the binary search returned.
  4. If it is, then you would do the lookup in your dictionary.
merlin2011
  • 71,677
  • 44
  • 195
  • 329
0
dict1 = {"Vendor": ("Cisco", "Juniper", "HP"), "Model": "2600", "RAM": "128", "IOS": "12.4", "Ports": "4"}
search = raw_input("What are you looking for : ")
looking = (dict1[search])
print looking
josliber
  • 43,891
  • 12
  • 98
  • 133
D' go
  • 21
  • 6