簡體   English   中英

比較兩組的最有效方法是什么?

[英]What is the most efficient way of comparing two sets?

在Python3中,比較兩個Set elements (是否具有相同元素)最省時方法是什么?

例如,我想要一個名為compareSets的函數,如下所示。 我應該如何編寫代碼以使其以最省時的方式工作?

def compareSets(a, b):
    # if (elements are identical)
    # return True

    # if (elements are not identical)
    # return False
    pass

要測試兩組是否相等,請使用相等運算符( == )。 這是REPL中的一個示例,它利用了可迭代的字符串:

>>> set('a') == set('a')
True
>>> set('a') == set('b')
False
>>> set('a') == set('ab')
False
>>> set('ba') == set('ab') #Shows the sets are order-independent
True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM