首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python集合操作奇怪的行为

Python集合操作奇怪的行为
EN

Stack Overflow用户
提问于 2020-12-20 19:33:46
回答 2查看 316关注 0票数 0

我有下面的代码片段

代码语言:javascript
复制
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)

每次我运行它时,它都会产生不同的输出。就像-

代码语言:javascript
复制
['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']

代码语言:javascript
复制
['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']

但我希望输出如下所示-

代码语言:javascript
复制
['apple', 'grapes', 'jackfruit', 'lychee', 'mango', 'orange', 'papaya', 'starfruit']
['grapes', 'mango']
['apple', 'orange', 'starfruit']
['jackfruit', 'lychee', 'papaya']
['apple', 'jackfruit', 'lychee', 'orange', 'papaya', 'starfruit']

由于这个顺序的改变,我再次没有通过一次代码竞赛考试。如果我错过了一些简单的事情或做了什么愚蠢的事情,请一定要让我知道。我希望输出按字典序排列。提亚

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-20 19:52:20

集合是无序的,所以你不能期望得到一致的顺序。因为您的预期输出是按排序顺序的,所以您可以简单地查看排序后的并集/交集等,如下所示:

代码语言:javascript
复制
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 ...
票数 2
EN

Stack Overflow用户

发布于 2020-12-20 19:51:41

Set是Python中的无序数据结构。假设您希望输出按字典顺序排列,只需在print命令中添加'sorted()‘即可:

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
['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']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65379416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档