简体   繁体   中英

How to compare these strings in python?

I have the following string:

1679.2235398,-1555.40390834,-1140.07728186,-1999.85500108

and I'm using a steganography technique to store it in an image. Now when I retrieve it back out of the image, sometimes I got it back in a complete form and I have no issue with that. Where in other occasions, the retrieved data are not fully retrieved (due to a modification/alteration being occurred on the image), so the result something look like this:

1679.2235398,-1555.I8\xf3\x1cj~\x9bc\x13\xac\x9e8I>[a\xfdV#\x1c\xe1\xea\xa0\x8ah\x02\xed\xd1\x1c\x84\x96\xe2\xfbk*8'l

Notice that, only "1679.2235398,-1555." are correctly retrieved, while the rest is where the modification has been occurred. Now, how do I compute (in percentage) how much I successfully retrieved? Since the length is not the same, I can't do a character by character comparison , it seems that I need to slice or convert the modified data into some other form to match the length of the original data.

Any tips?

A lot of this is going to depend on the context of your problem, but you have a number of options here.

If your results always look like that, you could just find the longest common subsequence , then divide by the length of the original string for a percentage.

Levenshtein distance is a common way of comparing strings, as the number of characters required to change to turn one string into another. This question has several answers discussing how to turn that into a percentage.

If you don't expect the strings to always come out in the same order, this answer suggests some algorithms used for DNA work.

Well it really depends.. My solution would be something like this:

I would start with all the longest string possible and check if they are in the new string if original_string in new_string: 'something happens here'. that would be inside a loop that wld decrease the size of the original string and get all combinations possible. so the next one wld be N-1 long and have 2 possible combinations (cutting off the first number or the last number), and so on, until u get to a specific threshold, or to 1 long strings.
the loop can store the longest string you find in a log inside the if conditional, and afterward you can just check the results. hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM