- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如果我有这样一个列表
my_list = [1, 7, 3, 4, 2, 9, 6, 5, 8]
我还有其他长度小于 my_list
的列表。例如,假设我有如下两个其他列表:
my_other_list_1 = [1, 7, 3, 4, 2]
my_other_list_2 = [1, 7, 4, 9, 8]
所有列表都有不同的元素,其他列表有来自原始列表的元素。
我的问题是找到在 my_list
中具有最长匹配项的列表(my_other_list_1
或 my_other_list_2
)。首先请问这个问题叫什么?然后,我更喜欢获得每个列表的最长匹配项的长度。我该怎么做?
在这个例子中,我会返回 my_other_list_1
因为它有一个长度为 5
的匹配,因为 [1, 7, 3, 4, 2]
已经在 my_list
中。另外,我会为 my_other_list_2
返回 2
,因为其中有一个长度为 2
的匹配项,即 [1, 7]
。
总结一下,如果我有一个算法 A 并且输入是 my_list
、my_other_list_1
和 my_other_list_2
,该算法应该返回
my_other_list_1 has match 5
my_other_list_2 has match 2
注意。我认为这是一个称为最长公共(public)子序列 (LCS) 问题的问题,但据我所知,在 LCS 问题中,子序列不需要是连续的。
最佳答案
def longest_match(main_list, *other_lists, **options):
try:
if options:
ordered = options['ordered']
else:
raise Exception
except:
ordered = False
best_match = 0
longest_match = 0
for index, list in enumerate(other_lists):
current_match = 0
if not ordered:
while True:
if list[:current_match] == main_list[:current_match]:
current_match += 1
else:
if current_match > longest_match:
longest_match = current_match
best_match = index
break
else:
for i, letter in enumerate(list):
current_match = i
if letter == main_list[0]:
current_match += 1
while True:
if list[i:current_match] == main_list[:current_match]:
current_match += 1
else:
if current_match > longest_match:
longest_match = current_match
best_match = index
break
return other_lists[best_match]
print longest_match(my_list, my_other_list_1, my_other_list_2, ordered=True) #kwarg odered=True will allow for mid list comprehension
我在手机上并没有测试以下内容,但它应该可以解决问题!
关于python - 如何从 Python 中的一组列表中找到最长的匹配项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37706839/
我正在尝试编写一个名为 map-longest 的 Clojure 实用函数(感谢备用名称建议)。该函数将具有以下“签名”: (map-longest fun missing-value-seq c1
为什么我创建了一个重复的线程 我在阅读后创建了这个线程 Longest increasing subsequence with K exceptions allowed .我意识到提出问题的人并没有真
我正在编写一个 Sub 来识别 1 到 1000 之间最长的 Collatzs 序列。由于我刚刚开始学习 VBA,我想知道如何添加过程来计算每个序列的长度。 Sub Collatz() Dim i
我正在编写一个 Sub 来识别 1 到 1000 之间最长的 Collatzs 序列。由于我刚刚开始学习 VBA,我想知道如何添加过程来计算每个序列的长度。 Sub Collatz() Dim i
我正在尝试减去 CSV 中的两列以创建第三列“持续时间”结束时间 - 开始时间 每一行也对应一个用户 ID。 我可以创建一个仅包含“持续时间”列的 csv 文件,但我宁愿将其重定向回原始 csv。 例
我在 2018.04 玩这个最长的 token 匹配,但我认为最长的 token 不匹配: say 'aaaaaaaaa' ~~ m/ | a+? | a+ /; # 「a」
因此,按照规范规定最终用户/应用程序提供的给定变量(200 字节)的字节长度。 使用 python 字符串,字符串的最大字符长度是多少,满足 200 字节,因此我可以指定我的数据库字段的 max_le
我需要针对我们的Jenkins构建集群生成每周报告。报告之一是显示具有最长构建时间的作业列表。 我能想到的解决方案是解析每个从属服务器(也是主服务器)上的“构建历史”页面,对于作业的每个构建,都解析该
我正在构建一个 iOS 应用程序,它将流式传输最长为 15 秒的视频。我阅读了有关 HLS 的好文章,因此我一直在对片段大小为 5 秒的视频进行转码。如果视频的第一部分加载时间太长,那么我们可以在接下
docs for Perl 6 longest alternation in regexes punt to Synopsis 5记录 longest token matching 的规则.如果不同的
我是一名优秀的程序员,十分优秀!