我知道任何带有空集的python集union
都会导致它自己。但是当union
在for循环中时我发现了一些奇怪的行为。
num= set([2,3,4])
emp= set()
print num|emp
>>>set([2, 3, 4])
s = set()
inp = ["dr101-mr99","mr99-out00","dr101-out00","scout1-scout2","scout3- scout1","scout1-scout4","scout4-sscout","sscout-super"]
for ele in inp:
r = set(ele.split("-"))
print r
s.union(r)
print s
>>>set(['mr99', 'dr101'])
set(['out00', 'mr99'])
set(['out00', 'dr101'])
set(['scout1', 'scout2'])
set(['scout1', 'scout3'])
set(['scout4', 'scout1'])
set(['scout4', 'sscout'])
set(['super', 'sscout'])
set([])
任何人都可以告诉我为什么最后一集是空的? 输出应该是集合中的每个唯一元素吗?
答案 0 :(得分:27)