我对集合有个小问题。所以我有一个叫做s的集合:
s = set(['Facebook', 'Yahoo', 'Gmail'])
我有一个叫做l的列表:
l = ['Yahoo', 'Google', 'MySpace', 'Apple', 'Gmail']
如何检查集合s中的哪些内容在我的列表l中?你知道吗
我也尝试过这样做,但是Python给了我一个错误:
TypeError: 'set' object does not support indexing
因此,如果集对象不支持索引,如何编辑集对象的每个部分?你知道吗
谢谢。你知道吗
为什么不试试这个作为你的第一个问题
对于第二个问题,我不明白您到底想做什么,但我认为只要一个带有项的简单for循环就可以做到,或者您可以使用
iter(s)
或enumerate(s)
如果你一定需要索引(以为那些不是索引)那是更有效的方法。就你而言:
下面是效率较低的方法:
另外,不要像这样声明一个集合:
试试这个:
只是为了节省一些时间:)
测试交叉点:
演示:
你也可以用
for
循环来循环你的集合,但这并没有那么有效。你知道吗相关问题 更多 >
编程相关推荐