简体   繁体   English

Python - 比较两个字符串/列表中“正确”顺序排序的单词#的最有效方法

[英]Python - Most efficient way to compare # of words sequenced in “right” order across two strings/lists

I was wondering what the most computationally efficient Python way of cracking this problem would be. 我想知道解决这个问题的计算效率最高的Python方法是什么。

Say you have two strings (or lists from splitting those strings--doesn't matter), "this is the right string" vs. "this is right the string." 假设您有两个字符串(或拆分这些字符串的列表,没关系),“这是正确的字符串”与“这是正确的字符串”。

We're assuming that the first string is always right, and a score will be assigned to the second string based on which words were sequenced in the right order. 我们假设第一个字符串总是正确的,并且将根据单词以正确的顺序对第二个字符串分配一个分数。 For the above two strings, we would assign a score of 0.6 (as only 3 of the 5 words are in the right position). 对于上面两个字符串,我们将分配0.6(因为5个单词中只有3个在正确的位置)。

Best, Georgina 最好的,乔治娜

This sounds very much like homework. 这听起来非常像家庭作业。 Try thinking about it for a bit. 尝试考虑一下。 Would it suffice to traverse the list of correct words once, and check if the corresponding word in the second list is equal to the word in the correct list? 遍历正确单词列表一次并检查第二个列表中的相应单词是否等于正确列表中的单词是否足够?

I would probably zip the lists in python and compare the pairs for equality. 我可能会在python中压缩列表并比较这些对是否相等。

a = "this is the right string"
b = "this is right the string"

sum([1 for i,v in zip(a.split(), b.split()) if i == v])
sum(f == s for f, s in zip(first, second)) / len(first)

Use ord() to convert each character to an integer value (its ordinal value), and then XOR each character together using the bitwise operator ^ . 使用ord()将每个字符转换为整数值(其序数值),然后使用按位运算符^将每个字符异或。 If the characters are the same, the XOR operation will return 0 (zero), then use |= to bitwise OR the returned value with result and then save the result of the operation as result . 如果字符相同,则XOR操作将返回0 (零),然后使用|=对返回的值与result进行位或result ,然后将操作的结果保存为result If result is still zero after you iterate over all the characters, then the strings are equivalent. 如果在遍历所有字符后result仍为零,则字符串是等效的。

a = "this is the right string"
b = "this is right the string"

result = 0
for x,y in zip(a,b):
    result |= ord(x) ^ ord(b)

(if result == 0): print "Equivalent"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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