我是python的新手,我很困惑。有人能给我解释一下从列表中生成一个集合的两种变体的区别吗?哪一个更正确?你知道吗
a = ["Jake", "John", "Eric"]
b = ["John", "Jill"]
c = set([])
d = set([])
for i in range (len(a)):
c.add(a[i])
for y in range (len(b)):
d.add(b[y])
print c.difference(d)
import sets
e= sets.Set(a)
print e
f = sets.Set(b)
print f
print e.difference(f)
Outcome
set(['Jake', 'Eric'])
Set(['Jake', 'Eric'])
唐克斯!你知道吗
将列表转换为集合不需要for循环:
使用
set
对象而不是sets.Set()
,sets.Set()
已被否决。你知道吗相关问题 更多 >
编程相关推荐