Hello!
There is a file with lines in which you need to search for a line by the value entered by the user. The contents of the file of this type:
45 123 0000 77788 77789 999900
That is, all the lines of different lengths. The user enters a number that is larger than the number in the database. That is, for example, if he enters 450, 45000123 or 45777, then the first line should be found, if he enters 77788013 or 7778888, then the fourth and so on.
I tried to use the find () function, but it searches only for the exact value. Difflib also did not help me.
There is an idea to sort through each character string and user input, where, by the maximum of matches, I find out the length of the field in the database, then limit the user input with this length and use the find () function, but I don’t know how to implement it.
More examples:
3766 376690 3767 3768 3769 971 9712 971200 9712234 97124 97125 97126 971287 971288
The person enters the number "3766823013" - is "3766".
Introduces "3766900124" - is located "376690". Enters "9712473023" - the string "97124" is located.
Enters the "97122348230" is located - "9712234".
The file is large - 150 MB of such lines.
The answer was correct:
filestr.find(userstr)==0 or userstr.find(filestr)==0
It works absolutely, as it should - do not take away, nor add.
PS Me, too, Ivanov Yura is called - that's a coincidence, right? :)
Comrade @ReinRaus , I’m not misleading anyone. The find () method really looks for only an exact match (at least it works for me that way), but once I replaced it with filestr.find (userstr) == 0 or userstr.find (filestr) == 0 and as if by magic It all worked as it should. Tell me, where have I confused you?
@ @ReinRaus , so you lay out the version of the program (both)? Exactly that I rechecked everything 2 times: just find () doesn't work
@ReinRaus , and then I’ll post the BOTH version of the program (for doubters). http://dpaste.com/726715/ - with just find ()
http://dpaste.com/726716/ - with filestr.find (userstr) == 0 or userstr.find (filestr) == 0
http://dpaste.com/726719/ - a piece of db.txt (the file itself weighs 156 MB)
And no one accidentally tells you how to optimize the search for such a database? For 156 mb looking for seconds 20 somewhere in me.