[英]Merge Sort 2 Lists Finding Common Elements
对于一项分配,我们被要求编写一个函数,该函数接受2个列表作为输入,然后返回一个包含“列表1”和“列表2”中所有名称的列表。
已经要求使用基于合并排序的算法来完成。 到目前为止,我所获得的返回正确的列表,但是我进行了太多比较以获取此列表。 VoterList是给我们的指定类,因此我们不使用常规的python列表。 只能将VoterName对象(组成两个输入列表的对象)附加到VoterList。 传入的列表均按字典顺序排列。
欢迎提供任何有关减少我的比较的建议。 谢谢。
from classes import VoterList
def fraud_detect_merge(first_booth_voters, second_booth_voters):
fraud = VoterList()
length_first = len(first_booth_voters)
length_second = len(second_booth_voters)
first_booth_position = 0
second_booth_position = 0
comparisons = 0
while first_booth_position < length_first:
if second_booth_position == length_second:
first_booth_position += 1
second_booth_position = 0
else:
name_comparison = first_booth_voters[first_booth_position]
comparisons += 1
if second_booth_voters[second_booth_position] == name_comparison:
fraud.append(second_booth_voters[second_booth_position])
first_booth_position += 1
second_booth_position = 0
else:
second_booth_position += 1
return fraud, comparisons
您输入的内容尚不清楚,是否已经排序? 您会收到清单。 查看可以对列表执行的操作,您将找到pop()
操作。 这将从列表中删除一项,并提供其价值。 由于列表都是按顺序排列的,因此可以使用以下方法:
def fraud_detect_merge(first_booth_voters, second_booth_voters):
fraud = VoterList()
comparisons = 0
first_booth_voters.sort() # if not already sorted
second_booth_voters.sort() # if not already sorted
first = first_booth_voters[0]
second = second_booth_voters[0]
while first_booth_voters and second_booth_voters:
comparisons += 1
if first == second:
fraud.append(first)
first = first_booth_voters.pop(0)
second = second_booth_voters.pop(0)
elif first < second:
first = first_booth_voters.pop(0)
else:
second = second_booth_voters.pop(0)
return fraud, comparisons
分配恰好要求您找到一个解决方案,并且有很大的合并排序提示,因此我不会为您提供答案:)但是也许我可以指出您的代码中正在发生的事情:使用while循环在最坏的情况下,您基本上完成了两个长度为length_first
和length_second
嵌套循环:
for name_comparison in first_booth_voters:
for second_name in second_booth_voters:
comparisons += 1
if second_name == name_comparison:
fraud.append(second_name)
break
在最坏的情况下,将导致length_first x length_second
比较。 鉴于输入列表已排序,这当然不是最佳选择。 您需要利用排序的优势。 而且,如果输入列表未排序,则应考虑使用更具可读性的嵌套for循环替换难以阅读/调试/理解while循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.