我有两张单子。第一个是字符串列表。第二个是字符串元组的列表。假设我有第一个列表中的字符串s
。我想在第二个列表中找到s
按字母顺序排列的所有对。一个具体的例子:
s = "QZ123DEF"
("QZ123ABC", "QZ125ZEQ") # would return as a positive match
("QF12", "QY22") # would not return as a positive match
我想到了一种蛮力方法,即检查第二个列表中所有元组的s
是否大于第一个字符串而小于1秒,但我想知道是否有更好的方法。顺便说一下,我使用的是python。在
因此,假设元组中只有两个条目,您可以稍微理解一下:
这可以扩展为
^{pr2}$s
的列表,结果存储在dict
中:也许。。。但它仍然是“蛮力”
这里有一种使用对分模块的方法,这需要先对
S
排序:输出:
^{pr2}$与随机数据上的暴力方法相比,该方法快2-3倍:
定时比较:
相关问题 更多 >
编程相关推荐