集和子集 [英] sets and subsets
问题描述
通过使用列表,我可以创建一组数字。假设我有三个列表。
一个列表是超集,一个是包含所有数字的集合
(就像超集)和最后一个是超集的子集。对于
例如:
a = [1,2,3,4,5]#超级套装。
b = [1,2,3,4,5]#看起来就像超级套装,但它不是。
c = [2,4] #A子集
我想删除2& 4来自b组,因为它们出现在集合中......
这会使这些集合看起来像这样:
a = [1,2,3 ,4,5]
b = [1,3,5]
c = [2,4]
我如何测试集c找到它包含的内容,然后查看集合b
看看它是否包含任何相同的数字,如果是,请删除它们。
By using lists, I can create sets of number. Suppose I have three lists.
One list is the super-set, one is a set that contains all the numbers
(just like the super-set) and the last is sub-set of the super-set. For
example:
a = [1,2,3,4,5] # The super-set.
b = [1,2,3,4,5] # Looks just like the super-set, but it''s not.
c = [2,4] # A sub-set
I''d like to remove 2 & 4 from set b BECAUSE they are present in set c...
this would make the sets look like this:
a = [1,2,3,4,5]
b = [1,3,5]
c = [2,4]
How do I test set c to find what it contains and then look at set b to
see if it contains any of those same numbers, and if so, remove them.
推荐答案
我相信有更优雅的方法可以做到这一点,但这里有一个
解决方案。
>
for c中的项目:
如果b.count(item)> 0:
b.remove(item)
" Bart Nessux" < BA ********* @ hotmail.com>在消息中写道
news:c0 ********* @ solaris.cc.vt.edu ...
I am sure there is a much more elegant way to do this, but here is one
solution.
for item in c:
if b.count(item) > 0:
b.remove(item)
"Bart Nessux" <ba*********@hotmail.com> wrote in message
news:c0*********@solaris.cc.vt.edu...
通过使用列表,我可以创建集合数量。假设我有三个列表。
一个列表是超集,一个是包含所有数字的集合
(就像超级集合),最后一个是超级集合的子集组。对于
例如:
a = [1,2,3,4,5]#超级集。
b = [1,2,3,4,5 ]#看起来就像超级套装,但它不是。
c = [2,4] #A子集
我想删除2& ; 4来自b组,因为它们出现在集合中......
这会使集合看起来像这样:
a = [1,2,3,4,5]
b = [1,3,5]
c = [2,4]
如何测试集合c以找到它包含的内容然后将集合b看到
查看它是否包含任何相同的数字,如果是,则删除它们。
By using lists, I can create sets of number. Suppose I have three lists.
One list is the super-set, one is a set that contains all the numbers
(just like the super-set) and the last is sub-set of the super-set. For
example:
a = [1,2,3,4,5] # The super-set.
b = [1,2,3,4,5] # Looks just like the super-set, but it''s not.
c = [2,4] # A sub-set
I''d like to remove 2 & 4 from set b BECAUSE they are present in set c...
this would make the sets look like this:
a = [1,2,3,4,5]
b = [1,3,5]
c = [2,4]
How do I test set c to find what it contains and then look at set b to
see if it contains any of those same numbers, and if so, remove them.
b = [x表示x中的x如果x不在c]
" Bart Nessux" < BA ********* @ hotmail.com>在留言中写道
news:c0 ********* @ solaris.cc.vt.edu ...
|通过使用列表,我可以创建数字集。假设我有三个列表。
|一个列表是超级集,一个是包含所有数字的集合
| (就像超级集合一样),最后一个是超集的子集。对于
|例如:
|
| a = [1,2,3,4,5]#超级套装。
| b = [1,2,3,4,5]#看起来就像超级套装,但它不是。
| c = [2,4] #A子集
|
|我想删除2&来自b组的4,因为他们出现在集合中......
|这会使套装看起来像这样:
|
| a = [1,2,3,4,5]
| b = [1,3,5]
| c = [2,4]
|
|如何测试set c以查找它包含的内容,然后将set b看作
|看看它是否包含任何相同的数字,如果是,请删除它们。
|
b=[x for x in b if x not in c]
"Bart Nessux" <ba*********@hotmail.com> wrote in message
news:c0*********@solaris.cc.vt.edu...
| By using lists, I can create sets of number. Suppose I have three lists.
| One list is the super-set, one is a set that contains all the numbers
| (just like the super-set) and the last is sub-set of the super-set. For
| example:
|
| a = [1,2,3,4,5] # The super-set.
| b = [1,2,3,4,5] # Looks just like the super-set, but it''s not.
| c = [2,4] # A sub-set
|
| I''d like to remove 2 & 4 from set b BECAUSE they are present in set c...
| this would make the sets look like this:
|
| a = [1,2,3,4,5]
| b = [1,3,5]
| c = [2,4]
|
| How do I test set c to find what it contains and then look at set b to
| see if it contains any of those same numbers, and if so, remove them.
|
你去...列表理解。这绝对更好看。
Elaine Jackson < EL *************** @ home.com>在消息中写道
news:zcxWb.464065
There you go... list comprehension. That is definatly nicer to look at.
"Elaine Jackson" <el***************@home.com> wrote in message
news:zcxWb.464065
这篇关于集和子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!