Home Backend Development Python Tutorial Python中集合的内建函数和内建方法学习教程

Python中集合的内建函数和内建方法学习教程

Jun 06, 2016 am 11:14 AM
python function method gather

集合内建函数和内建方法
(1)标准类型函数
       len():把集合作为参数传递给内建函数 len(),返回集合的基数(或元素的个数)。
(2)集合类型工厂函数
       set()和 frozenset()工厂函数分别用来生成可变和不可变的集合。如果不提供任何参数,默认会生成空集合。如果提供一个参数,则该参数必须是可迭代的,即一个序列或迭代器或支持迭代的一个对象,例如一个文件或一个字典。
(3)方法(所有的集合方法)
s.issubset(t)                  如果s是t的子集,则返回True,否则返回False
s.issuperset(t)               如果t是s的超集,则返回True,否则返回False
s.union(t)                       返回一个新集合,该集合是s和t的并集
s.intersection(t)            返回一个新集合,该集合是s和t的交集
s.difference(t)               返回一个新集合,该集合是 s 的成员,但不是 t 的成员
s.symmetric_difference(t)     返回一个新集合,该集合是s或t的成员,但不是s和t共有的成员
s.copy()                         返回一个新集合,它是集合s的浅复制
       内建方法copy() 没有等价的操作符。和同名的字典方法一样,copy()方法比用像set()、frozenset()或dict()这样的工厂方法复制对象的副本要快。
(4)方法(仅适用于可变集合)

可变集合类型的方法:

2015819113320504.png (678×274)

演示实例:
一、集合类型方法

2015819113444190.jpg (886×254)

>>> s = set('cheeseshop')
>>> t = set('bookshop')
>>> s
set(['c', 'e', 'h', 'o', 'p', 's'])
>>> t
set(['b', 'h', 'k', 'o', 'p', 's'])
>>> s.issubset(t)
False
>>> s.issuperset(t)
False
>>> s.union(t)
set(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's'])
>>> s.intersection(t)
set(['h', 's', 'o', 'p'])
>>> s.difference(t)
set(['c', 'e'])
>>> s.symmetric_difference(t)
set(['b', 'e', 'k', 'c'])
>>> s.copy()
set(['p', 'c', 'e', 'h', 's', 'o'])

Copy after login


二、可变集合类型的方法

1、s.update(t)——用t中的元素修改s,即s现在包含s或t的成员。

>>> s.update(t)
>>> s
set(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's'])

Copy after login

2、s.intersection_update(t)——s中的成员是共同属于s和t中的元素。

>>> s = set('cheeseshop')
>>> t = set('bookshop')
>>> s.intersection_update(t)
>>> s
set(['h', 's', 'o', 'p'])

Copy after login


3、s.difference_update(t)——s中的成员是属于s但不包含在t中的元素。

>>> s = set('cheeseshop')
>>> t = set('bookshop')
>>> s.difference_update(t)
>>> s
set(['c', 'e'])

Copy after login


4、s.symmetric_difference_update(t)——s中的成员更新为那些包含在s或t中,但不是s和t共有的元素。

>>> s = set('cheeseshop')
>>> t = set('bookshop')
>>> s.symmetric_difference_update(t)
>>> s
set(['c', 'b', 'e', 'k'])

Copy after login


5、s.add(obj)——在集合s中添加对象obj。

>>> s.add('o')
>>> s
set(['c', 'b', 'e', 'k', 'o'])

Copy after login


6、s.remove(obj)——从集合s中删除对象obj,如果obj不是集合s中的元素(obj not in s),将引发KeyError。

<p>>>> s.remove('b')
>>> s
set(['c', 'e', 'k', 'o'])
>>> s.remove('a')</p><p>Traceback (most recent call last):
 File "<pyshell#53>", line 1, in <module>
  s.remove('a')
KeyError: 'a'
</p>

Copy after login


7、s.discard(obj)——如果obj是集合s中的元素,从集合s中删除对象obj。

>>> s.discard('a')
>>> s
set(['c', 'e', 'k', 'o'])
>>> s.discard('e')
>>> s
set(['c', 'k', 'o'])

Copy after login

8、s.pop()——删除集合是中的任意一个对象,并返回它。

>>> s.pop()
'c'
>>> s
set(['k', 'o'])

Copy after login

9、s.clear()——删除集合s中的所有元素。

>>> s.clear()
>>> s
set([])

Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles