`

Python ---- Sets

 
阅读更多
Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

Here is a brief demonstration:

>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> fruit = set(basket)               # create a set without duplicates
>>> fruit
set(['orange', 'pear', 'apple', 'banana'])
>>> 'orange' in fruit                 # fast membership testing
True
>>> 'crabgrass' in fruit
False

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
set(['a', 'r', 'b', 'c', 'd'])
>>> a - b                              # letters in a but not in b
set(['r', 'd', 'b'])
>>> a | b                              # letters in either a or b
set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
>>> a & b                              # letters in both a and b
set(['a', 'c'])
>>> a ^ b                              # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
分享到:
评论

相关推荐

    MySQL-python-1.2.2.win32-py2.6.exe

    自己用VC++6.0编译的 MySQL-python,需要 MySQL-5.1.34 的 libMySQL.dll 和Python2.6的“Set”有点兼容性问题,自己照着改一下: http://sourceforge.net/forum/message.php?msg_id=5808948 1)__init__.py 删除 ...

    Python-kubesql一个使用sql查询kubernetes资源的工具

    它将Kubernetes的各种资源,如节点(Nodes)、Pods、服务(Services)、部署(Deployments)、副本集(ReplicaSets)等,抽象为数据库表的形式。这样,用户就可以通过标准的SQL查询语句来检索、过滤、聚合这些资源的...

    python-3.7.0 Windows64位官方安装包

    2. 不可变集合(Frozen Sets):增加了对不可变集合的支持,可以创建不可变的set对象。 3. 异常链(Exception Chaining):改进了异常处理,可以显示完整的错误堆栈信息。 4. 更好的类型注解(Improved Type ...

    SERFON_python-master.zip

    The whole project was wirtten by python3,it includes 2 codes and 2 sorts of data sets which named iris and wbc(all of them can get from uci website easily),you can train the SNN model and test it via...

    Python-redispyRedis的Python客户端

    **Python-redis-py:Redis的Python客户端** `redis-py`是Python社区广泛使用的Redis数据库的客户端库,它提供了一种简单而高效的方式来与Redis数据存储进行交互。Redis是一种高性能的键值对数据存储系统,常用于...

    data-science-using-python-r

    In the Hands-on Analysis exercises, readers are challenged to solve interesting business problems using real-world data sets. 通过数据科学学习数据科学! 数据科学使用Python和R将使您进入世界上两个最...

    python-3.6.1 Tutorial

    - **Sets(集合)** - 介绍集合的基本概念及其特点。 - 讨论如何使用集合进行集合运算。 - **Dictionaries(字典)** - 详解字典的数据结构,包括键值对的概念。 - 探讨字典的操作方法。 - **Looping Techniques...

    python-programmers-artificial-intelligence-studies

    Collections: Lists, tuples, dictionaries, sets, NumPy arrays, pandas Series & DataFrames Static, dynamic and interactive visualizations Data experiences with real-world datasets and data sources Intro...

    简明-Python-教程.doc

    - **高效的数据结构**:Python内置了许多高效的数据结构,例如列表(lists)、字典(dictionaries)、集合(sets)等,这些数据结构使得处理复杂数据变得简单高效。 - **动态类型**:Python是一种动态类型的编程语言,这...

    Python-mongoctl管理MongoDB服务器和副本设置使用JSON配置

    Python-mongoctl是用于管理MongoDB服务器和副本集的工具,它允许用户通过JSON配置文件进行灵活的设置和控制。本文将深入探讨如何使用mongoctl来管理MongoDB实例和副本集。 ### Python-mongoctl简介 Python-...

    Python-KaggleInstacart市场篮子分析

    Python-KaggleInstacart市场篮子分析 orders (3.4m rows, 206k users): order_id: order identifier user_id: customer identifier eval_set: which evaluation set this order belongs in (see SET described...

    Intermediate-Python-Cheat-Sheet.pdf

    5. **集合(Sets)** - `my_set = set(l)`:从列表`l`中创建一个只包含不重复元素的集合`my_set`。 - `len(my_set)`:返回集合`my_set`中元素的数量。 - `a in my_set`:如果值`a`存在于集合`my_set`中,则返回`...

    Intermediate-Python-Cheat-Sheet.zip

    - **集合(Sets)**:学习集合的交集、并集、差集和对称差集操作,如`intersection()`, `union()`, `difference()`, 和 `symmetric_difference()`。 - **列表推导式(List Comprehensions)**:掌握创建复杂列表的...

    Python库 | streamsets-3.5.0.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:streamsets-3.5.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Python库 | streamsets-3.10.0-cp36-cp36m-macosx_10_14_x86_64.whl

    资源分类:Python库 所属语言:Python 资源全名:streamsets-3.10.0-cp36-cp36m-macosx_10_14_x86_64.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    python-az:python-az

    1. **计算**:允许管理虚拟机(VMs)、虚拟机规模集(VM Scale Sets)、容器服务等。你可以创建、启动、停止、重启或删除 VM,并配置相关资源如网络接口和磁盘。 2. **存储**:提供了对 Azure Blob 存储、文件存储...

    python-data-structure-cn-master.zip

    4. 集合(Sets) 集合是无序的、不重复的元素集合,支持并集、交集、差集等数学运算。集合可以用来去重、测试成员关系或进行集合操作,如set.add()、set.remove()、set.intersection()等。 5. 堆(Heap) 堆是一种特殊...

    Python库 | streamsets-1.0.0.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:streamsets-1.0.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    python-2.4

    - **sets模块**:提供了集合数据类型的初步支持,用于不重复元素的存储和操作。 - **异常处理优化**:可以使用`raise E, None, None`来重新抛出一个已捕获的异常,无需额外的堆栈跟踪信息。 2. **标准库增强**: ...

Global site tag (gtag.js) - Google Analytics