简体   繁体   English

合并排序2列表以查找常见元素

[英]Merge Sort 2 Lists Finding Common Elements

For an assignment we have been asked to write a function that takes as inputs 2 lists and then returns a list containing all of names that were in both 'list 1' and 'list 2'. 对于一项分配,我们被要求编写一个函数,该函数接受2个列表作为输入,然后返回一个包含“列表1”和“列表2”中所有名称的列表。

It has been asked to be done using a merge sort based algorithm. 已经要求使用基于合并排序的算法来完成。 What I have got so far returns the correct list, however I am making too many comparisons to get this list. 到目前为止,我所获得的返回正确的列表,但是我进行了太多比较以获取此列表。 VoterList is a specified class given to us so that we don't use normal python lists. VoterList是给我们的指定类,因此我们不使用常规的python列表。 Only VoterName objects (what the two input lists are made of) are able to be appended to a VoterList. 只能将VoterName对象(组成两个输入列表的对象)附加到VoterList。 The lists being passed in are both in lexicographical order. 传入的列表均按字典顺序排列。

Any advice on how to reduce my comparisons is welcomed. 欢迎提供任何有关减少我的比较的建议。 Thank you. 谢谢。

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

It is not clear what your input is, is it already sorted? 您输入的内容尚不清楚,是否已经排序? You are being given lists. 您会收到清单。 Check out what operations can be performed on lists and you will find the pop() operation. 查看可以对列表执行的操作,您将找到pop()操作。 This removes one item from the list and give you its value. 这将从列表中删除一项,并提供其价值。 As the lists are both in order the following approach can be used: 由于列表都是按顺序排列的,因此可以使用以下方法:

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

The assignment exactly asks you find a solution, and there is the big hint of merge sort, so I'm not going to spill out an answer for you :) But perhaps I can point out what's happening in your code: with your while loop you've essentially done two nested loops of lengths length_first and length_second in worst case: 分配恰好要求找到一个解决方案,并且有很大的合并排序提示,因此我不会为您提供答案:)但是也许我可以指出您的代码中正在发生的事情:使用while循环在最坏的情况下,您基本上完成了两个长度为length_firstlength_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

That results in length_first x length_second comparisons in worst case. 在最坏的情况下,将导致length_first x length_second比较。 That is certainly not optimal given that the input lists are sorted. 鉴于输入列表已排序,这当然不是最佳选择。 You need to take advantage of the sortedness. 您需要利用排序的优势。 And if the input lists aren't sorted, you should consider replacing your harder-to-read/debug/understand while loop with more readable nested for loops. 而且,如果输入列表未排序,则应考虑使用更具可读性的嵌套for循环替换难以阅读/调试/理解while循环。

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

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