Python Set difference() 方法

Python 3 教程 · 2019-02-06 18:43:22

描述

difference() 方法用于返回集合的差集,即返回的集合元素包含在第一个集合中,但不包含在第二个集合(方法的参数)中。

语法

difference() 方法语法:

set.difference(set)

参数

  • set -- 必需,用于计算差集的集合

返回值

返回一个新的集合。

实例

返回一个集合,元素包含在集合 x ,但不在集合 y :

实例 1

x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.difference(y) print(z)

输出结果为:

{'cherry', 'banana'}

点击查看所有 Python 3 教程 文章: https://codercto.com/courses/l/10.html

查看所有标签

Dive Into Python 3

Dive Into Python 3

Mark Pilgrim / Apress / 2009-11-6 / USD 44.99

Mark Pilgrim's Dive Into Python 3 is a hands-on guide to Python 3 (the latest version of the Python language) and its differences from Python 2. As in the original book, Dive Into Python, each chapter......一起来看看 《Dive Into Python 3》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换