Python compares differences between two lists, same (difference set, intersection, and set)

Source: Internet
Author: User
Initializing data

listA = [‘zhangsan‘, ‘lisi‘, ‘wangwu‘]listB = [‘zhangsan‘, ‘lisi‘, ‘zhaoliu‘]

1, take the difference set
1.1, Lista corresponding to LISTB difference set

set(listA).difference(set(listB))-----set([‘wangwu‘])
1.2、listB对应listB的差集
set(listB).difference(set(listA))-----set([‘zhaoliu‘])

2, take the intersection

set(listA).intersection(set(listB))-----set([‘lisi‘, ‘zhangsan‘])

3. Take and set

set(listA).union(set(listB))-----set([‘lisi‘, ‘zhaoliu‘, ‘zhangsan‘, ‘wangwu‘])

For more usage, you can check the usage of the set yourself.
Https://docs.python.org/2/library/stdtypes.html?highlight=set#set

Python compares differences between two lists, same (difference set, intersection, and set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.