Python未全部运行
如何检查列表是否是更大列表的子集。
即
a = [1,2,3]
是 b = [1,2,3,4,5,6]
的子集
我可以做类似的事情吗
if a all in b
How do I check if a list is a subset of a bigger list.
i.e.
a = [1,2,3]
is a subset of b = [1,2,3,4,5,6]
Can I do something like
if a all in b
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://docs.python.org/library/stdtypes.html#set。是子集
http://docs.python.org/library/stdtypes.html#set.issubset
或
或(如果元素数量很重要)
or
or (if the number of elements is important)
主要喜欢其他答案,但我确实更喜欢这里的生成器语法,看起来更自然,并且它是惰性评估的:
如果您关心重复元素的数量,这个选项看起来不错,您可以优化它对 c 进行排序并使用二等分:
mostly like the other answers, but I do prefer the generator syntax here, seems more natural and it's lazily evaluated:
if you care about the number of repeated elements, this option seems nice, and you could optimize it sorting c and using bisect: