Question

I have a dictionary which i fetch from MySql DB, the ID is the key and the value is a string (relative Windows path of files to be exact). I have another list which has windows path as elements. Now, I need to match the list elements with the dictionary values and if exact match is found, i need to retrieve the key for that element, if not present, i'll update in the DB

My current code:

for line in f1:
        line = line.strip()
        try:
            index = next(k for (k,v) in self.db_file_list.iteritems() if line in v)
            file_list_csv.append(index)

The problem here is: Suppose value in my dictionary is "abc def", "a/b/c" then if i search values "abc" or "a/b" it would still match and give me key value which is not what i want.

Please help me out, i am a newbie to Python. Many thanks in advance!

~Saurav

Was it helpful?

Solution

If your only problem is that you would be matching substring matches as well, then replacing line in v with line == v would be enough (take care to use == in this case and not is: is does an identity comparison, whereas == simply compares values).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top