Python set 操作奇怪的行为

时间:2020-12-20 11:33:45

标签: python set

我有以下代码片段

seta = ["apple","orange","grapes","mango", "starfruit"]
setb = ["papaya","mango","jackfruit","grapes","lychee"]

def setOperation(seta, setb):
    union = set(seta) | set(setb)
    print(list(union)) 
    intersection = set(seta) & set(setb)
    print(list(intersection))
    difference = set(seta) - set(setb)
    print(list(difference))
    difference = set(setb) - set(seta)
    print(list(difference))
    sdifference = set(seta) ^ set(setb)
    print(list(sdifference))
    print(list(frozenset(set(seta))))
    
setOperation(seta,setb)

每次运行它都会产生不同的输出。 喜欢 -

['jackfruit', 'apple', 'mango', 'starfruit', 'grapes', 'lychee', 'orange', 'papaya']                                          
['grapes', 'mango']                                                                                                           
['orange', 'apple', 'starfruit']                                                                                              
['jackfruit', 'papaya', 'lychee']                                                                                             
['jackfruit', 'apple', 'starfruit', 'lychee', 'papaya', 'orange']                                                             
['orange', 'apple', 'grapes', 'mango', 'starfruit']


['grapes', 'mango', 'apple', 'orange', 'starfruit', 'lychee', 'papaya', 'jackfruit']                                          
['grapes', 'mango']                                                                                                           
['starfruit', 'apple', 'orange']                                                                                              
['lychee', 'papaya', 'jackfruit']                                                                                             
['apple', 'orange', 'starfruit', 'lychee', 'papaya', 'jackfruit']                                                             
['grapes', 'starfruit', 'mango', 'apple', 'orange']

但我希望输出如下 -

['apple', 'grapes', 'jackfruit', 'lychee', 'mango', 'orange', 'papaya', 'starfruit']
['grapes', 'mango']
['apple', 'orange', 'starfruit']
['jackfruit', 'lychee', 'papaya']
['apple', 'jackfruit', 'lychee', 'orange', 'papaya', 'starfruit']

由于这种顺序的变化,我再次未能通过一次代码竞赛考试。 如果我错过了一些简单的事情或做了一些愚蠢的事情,请告诉我。我想要按字典顺序输出。 TIA

2 个答案:

答案 0 :(得分:2)

集合是无序的,因此您不能期望获得一致的顺序。由于您的预期输出是按顺序排列的,您可以简单地查看已排序的联合/交集等:

seta = set(["apple","orange","grapes","mango", "starfruit"])
setb = set(["papaya","mango","jackfruit","grapes","lychee"])

print(sorted(seta | setb)) # Union
print(sorted(seta & setb)) # Intersection
# ... etc ...

答案 1 :(得分:1)

Set 是 Python 中的无序数据结构。鉴于您希望按字典顺序输出,只需在打印命令中添加“sorted()”:

seta = ["apple","orange","grapes","mango", "starfruit"]
setb = ["papaya","mango","jackfruit","grapes","lychee"]

def setOperation(seta, setb):
    union = set(seta) | set(setb)
    print(sorted(union)) 
    intersection = set(seta) & set(setb)
    print(sorted(intersection))
    difference = set(seta) - set(setb)
    print(sorted(difference))
    difference = set(setb) - set(seta)
    print(sorted(difference))
    sdifference = set(seta) ^ set(setb)
    print(sorted(sdifference))
    print(sorted(frozenset(set(seta))))
    
setOperation(seta,setb)

输出:

['apple', 'grapes', 'jackfruit', 'lychee', 'mango', 'orange', 'papaya', 'starfruit']
['grapes', 'mango']
['apple', 'orange', 'starfruit']
['jackfruit', 'lychee', 'papaya']
['apple', 'jackfruit', 'lychee', 'orange', 'papaya', 'starfruit']
['apple', 'grapes', 'mango', 'orange', 'starfruit']