bali
bali

Reputation: 337

Python set Operation strange behaviour

I have below code snippet

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)

which produces different different output everytime I run it. Like -

['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']

and


['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']

but I want the output as below -

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

Due to this change of order I am failing one code competition exam again. Please do let me know if I missed something simple or did anything stupid. I want the output in lexicographical order. TIA

Upvotes: 0

Views: 945

Answers (2)

IoaTzimas
IoaTzimas

Reputation: 10624

Set is an unordered data structure in Python. Given that you want the output in lexicographical order, just add 'sorted()' inside print commands:

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)

Output:

['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']

Upvotes: 1

ssp
ssp

Reputation: 1710

Sets are unordered so you can't expect to get a consistent order. Since your expected output is in sorted order, you can simply view the sorted union/intersection etc like so:

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 ...

Upvotes: 2

Related Questions