Let's say I have two lists: full_links
& href_links
I want to remove values in href_links
which contain duplicate values from full_links
For example:
full_links = [ 1,2,3,4,5]
href_links = [ 1,2,7,8,9,3]
So, the output should be:
comb_list = [7,8,9]
My code is:
comb_list = list(set(full_links) - set(href_links))
But it is not working.
It should be the other way round
>>> full_links = [ 1,2,3,4,5]
>>> href_links = [ 1,2,7,8,9,3]
>>> comb_list = list(set(full_links) - set(href_links))
>>> comb_list
[4, 5]
>>> comb_list = list(set(href_links) - set(full_links))
>>> comb_list
[8, 9, 7]
To take out/remove X
from Y
, you subtract X
from Y
: Y - X
comb_list = list(set(href_links) - set(full_links))
You want to remove (elements of) full_links
from href_link
, also called difference:
comb_list = list(set(href_links).difference(set(full_links)))
If you need to save the order,
>>> full_links = [1,2,3,4,5]
>>> href_links = [1,2,7,8,9,3]
>>> full_set = set(full_links)
>>> [i for i in href_links if i not in full_set]
[7, 8, 9]
Also, there is no need to construct 2 sets if you use the difference
method:
>>> list(set(href_links).difference(full_links))
[7, 8, 9]
因为它是倒退的!
comb_list = list(set(href_links) - set(full_links))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.