python - 在Python中查找两个列表之间单个不同元素的索引的有效方法

标签 python algorithm list xor

如果我有两个列表,每个列表的元素可以是两个可能值之一,并且这两个列表除了一个值外完全相同,我应该使用哪种方法来查找该值的索引?我将在 Python 中对此进行编码作为背景信息

方法一:

1. represent both lists as a binary literals  
2. use XOR on the two binary literals to give a value "v" that is a power of 2  
3. finally, use math.log(v, 2) to get the index

或方法B:

just iterate through both lists until a different element is found and 
get the index

还是使用 Python 的其他方式?

最佳答案

将您的列表转换为另一种表示形式将涉及遍历它们 - 还不如遍历以找到差异本身。

关于python - 在Python中查找两个列表之间单个不同元素的索引的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17500201/

相关文章:

python - Django 模型元类的标准文档字符串是什么?

python - 在 Django 中测试特定模型

python - 使用 pandas 高效读取大型 CSV 文件而不会崩溃

合并文件的 C 程序未找到 EOF,陷入无限循环!

python - 基于字典中的一个键的字典列表中的一个值的总和

python - Python 代码中的三个点 (...) 是什么意思,我在哪里可以看到完整的代码?

algorithm - 优化: Divide an array into continuous subsequences of length no greater than k such that sum of maximum value of each subsequence is minimum

arrays - 用最少的 x<y 比较对 4 个数字进行排序

字典列表键的 Python 总和

python - 如何将元组的元组转换为列表列表?