简体   繁体   English

Python:检查两个数组(可能包含重复元素)是否包含相同的元素集

[英]Python: Check if two arrays (may contain repeated elements) contain the same set of elements

I am trying to solve a homework problem. 我正在努力解决作业问题。

Q. Assume the existence of two arrays X and Y of m elements each. 问:假设每个m元素存在两个数组X和Y. Assume that they may possibly contain duplicates (ie, repeated elements), on which a total order relation is defined. 假设它们可能包含重复项(即重复元素),在其上定义了总顺序关系。 a) Develop an efficient algorithm for determining if X and Y contains the same set of elements. a)开发一种有效的算法,用于确定X和Y是否包含相同的元素集。

Now, to make this as efficient as possible, someone suggested using Hash tables. 现在,为了使其尽可能高效,有人建议使用哈希表。 I have been trying to implement it. 我一直在努力实现它。

I have already created the arrays and the hashtable, then I imported one array into the hashtable. 我已经创建了数组和哈希表,然后我将一个数组导入哈希表。

At this point I am looking for the most efficient way to search the array and give me the answer. 在这一点上,我正在寻找最有效的搜索阵列的方法,并给我答案。

dict = {'0':'-','1':'a','2':'b','3':'c'} #declare dictionary

print "first element of dict = ", dict['0']

print "\n"

array1 = ["4","5","6","7","8","9","10"]
print "array 1 = ", array1
array2 = ["4","5","6","7","8","9","10"]
print "array 2 = ", array2

print "\n"

print "array1[3] = ", array1[3]

print "\n"

print "clearing dictionary..."
dict.clear(); 

print "dict = ", dict

print "\n"

x = 0 #iterator for array1

print "importing array1 into dictionary..."

while x < len(array1) :
    dict[x] = array1[x]
    x += 1

print dict

y = 0 #iterator for array2

while y < len(array2) :
    if dict

If someone could kindly guide me further as to the logic I need here, that would be much appreciated. 如果有人能够进一步指导我在这里需要的逻辑,那将非常感激。

This is my solution. 这是我的解决方案。 Is this the most efficient solution? 这是最有效的解决方案吗? What would be the time complexity? 什么是时间复杂性? I am guessing O(n). 我在猜O(n)。 Am I correct? 我对么?

# define first array
array1 = ["1","1","1","2"]
print "array 1 = ", array1

#define second array
array2 = ["1","2","3"]
print "array 2 = ", array2

#function
def is_empty(x, y):

    #find set difference between first and second array
    difference1 = set(x) - set(y)
    print difference1

    #find set difference between second and first array
    difference2 = set(y) - set(x)
    print difference2

    #union two differences together
    finalset = difference1.union(difference2)
    print finalset

    #if there are elements in finalset, arrays do not contain same set of elements
    if finalset:
        print('The sets do not contain the same set of elements.')
        return False

    #if there are no elements in final set, arrays contain same set of elements
    else:
        print('The sets contain the same set of elements.')
        return True

#function call on two arrays
is_empty(array1, array2)

暂无
暂无

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

相关问题 检查两个字符串是否在Python中包含相同的单词集 - Check if two strings contain the same set of words in Python 删除包含2个相同元素的Python列表项 - Removing Python List Items that Contain 2 of the Same Elements 检查两个字符串是否在python中包含相同的模式 - Check if two strings contain the same pattern in python 返回包含数组重复元素的所有标签 - Return all labels that contain repeated elements of array 对于包含不同元素数量的两个一维数组,如何从一个数组中删除元素,使其大小与另一个数组相同? - For two 1D arrays that contain different numbers of elements, how to delete elements from one so it is the same size as the other? Python 拆分数组并检查两个 arrays 中所有元素的总和是否相同 - Python to split an array and check sum of all elements in two arrays is same 为什么用python编程的该数组中的所有元素都包含相同的字符串? - Why are all the elements in this array, programmed in python, contain the same string? Python:检查两个数据框是否在同一位置包含填充单元格 - Python: check if two dataframes contain filled cells in the same location 有没有办法检查两个 object 在 python 中的每个变量中是否包含相同的值? - Is there a way to check if two object contain the same values in each of their variables in python? 有没有一种方法可以检查两个列表是否在Python中包含相同的值? - Is there a way to check if two lists contain any of the same values in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM