用python设置背景音乐_用Python设置

本文介绍了Python中的集合(Set)概念,包括如何定义、操作集合以及集合的特性。集合是无序且不包含重复元素的数据结构,支持多种操作,如并集、交集、差集和对称差集。此外,文章还提到了冻结集(frozenset),它是不可变的集合类型,适用于需要不可变对象的场景。通过学习,读者将能够更好地理解和运用Python中的集合进行数据处理。
摘要由CSDN通过智能技术生成

用python设置背景音乐

Perhaps you recall learning about sets and set theory at some point in your mathematical education. Maybe you even remember Venn diagrams:

也许您回想起在数学教育中某个时候对集合集合理论的学习。 也许您甚至还记得维恩图:

Venn diagram

If this doesn’t ring a bell, don’t worry! This tutorial should still be easily accessible for you.

如果这没有敲响钟声,请不要担心! 您仍然可以轻松访问本教程。

In mathematics, a rigorous definition of a set can be abstract and difficult to grasp. Practically though, a set can be thought of simply as a well-defined collection of distinct objects, typically called elements or members.

在数学中,对集合的严格定义可能是抽象的且难以掌握。 但是实际上,集合可以简单地看作是明确定义的不同对象的集合,通常称为元素成员

Grouping objects into a set can be useful in programming as well, and Python provides a built-in set type to do so. Sets are distinguished from other object types by the unique operations that can be performed on them.

将对象分组到集合中在编程中也很有用,Python提供了内置的集合类型。 集可以通过对它们执行的唯一操作来与其他对象类型区分开。

Here’s what you’ll learn in this tutorial: You’ll see how to define set objects in Python and discover the operations that they support. As with the earlier tutorials on lists and dictionaries, when you are finished with this tutorial, you should have a good feel for when a set is an appropriate choice. You will also learn about frozen sets, which are similar to sets except for one important detail.

这是在本教程中将学到的内容:您将看到如何在Python中定义集合对象以及发现它们支持的操作。 与列表和字典的早期教程一样,当您完成本教程的学习时,应该对选择合适的集合有很好的感觉。 您还将了解冻结的集合 ,除了一个重要的细节外,它们与集合类似。

定义一个集合 (Defining a Set)

Python’s built-in set type has the following characteristics:

Python的内置set类型具有以下特征:

  • Sets are unordered.
  • Set elements are unique. Duplicate elements are not allowed.
  • A set itself may be modified, but the elements contained in the set must be of an immutable type.
  • 集是无序的。
  • 设置元素是唯一的。 不允许重复的元素。
  • 集合本身可以被修改,但是集合中包含的元素必须是不可变的类型。

Let’s see what all that means, and how you can work with sets in Python.

让我们看看这意味着什么,以及如何在Python中使用集合。

A set can be created in two ways. First, you can define a set with the built-in set() function:

可以通过两种方式创建集合。 首先,您可以使用内置的set()函数定义一个集合:

 x x = = setset (( << iteriter >> )
)

In this case, the argument <iter> is an iterable—again, for the moment, think list or tuple—that generates the list of objects to be included in the set. This is analogous to the <iter> argument given to the .extend() list method:

在这种情况下,参数<iter>是可迭代的(目前还是思想列表或元组),它生成要包含在集合中的对象列表。 这类似于为.extend()列表方法提供的<iter>参数:

Strings are also iterable, so a string can be passed to set() as well. You have already seen that list(s) generates a list of the characters in the string s. Similarly, set(s) generates a set of the characters in s:

字符串也是可迭代的,因此字符串也可以传递给set() 。 您已经看到list(s)生成字符串s中的字符列表。 类似地, set(s)生成一组中的字符的s

 >>> >>>  s s = = 'quux'

'quux'

>>> >>>  listlist (( ss )
)
['q', 'u', 'u', 'x']
['q', 'u', 'u', 'x']
>>> >>>  setset (( ss )
)
{'x', 'u', 'q'}
{'x', 'u', 'q'}

You can see that the resulting sets are unordered: the original order, as specified in the definition, is not necessarily preserved. Additionally, duplicate values are only represented in the set once, as with the string 'foo' in the first two examples and the letter 'u' in the third.

您会看到结果集是无序的:定义中指定的原始顺序不一定要保留。 此外,重复值仅在集合中表示一次,如前两个示例中的字符串'foo'和第三个示例中的字母'u'

Alternately, a set can be defined with curly braces ({}):

或者,可以用花括号( {} )定义一个集合:

When a set is defined this way, each <obj> becomes a distinct element of the set, even if it is an iterable. This behavior is similar to that of the .append() list method.

当以这种方式定义一个集合时,每个<obj>成为该集合的不同元素,即使它是可迭代的。 此行为类似于.append()列表方法的行为。

Thus, the sets shown above can also be defined like this:

因此,上面显示的集合也可以这样定义:

 >>> >>>  x x = = {
     {
      'foo''foo' , , 'bar''bar' , , 'baz''baz' , , 'foo''foo' , , 'qux''qux' }
}
>>> >>>  x
x
{'qux', 'foo', 'bar', 'baz'}

{'qux', 'foo', 'bar', 'baz'}

>>> >>>  x x = = {
     {
      'q''q' , , 'u''u' , , 'u''u' , , 'x''x' }
}
>>> >>>  x
x
{'x', 'q', 'u'}
{'x', 'q', 'u'}

To recap:

回顾一下:

  • The argument to set() is an iterable. It generates a list of elements to be placed into the set.
  • The objects in curly braces are placed into the set intact, even if they are iterable.
  • set()的参数是可迭代的。 它生成要放入集合中的元素列表。
  • 大括号中的对象即使可以迭代也可以原样放置到集合中。

Observe the difference between these two set definitions:

观察这两个集合定义之间的区别:

A set can be empty. However, recall that Python interprets empty curly braces ({}) as an empty dictionary, so the only way to define an empty set is with the set() function:

一组可以为空。 但是,请记住,Python将空花括号( {} )解释为空字典,因此定义空集的唯一方法是使用set()函数:

 >>> >>>  x x = = setset ()
()
>>> >>>  typetype (( xx )
)
<class 'set'>
<class 'set'>
>>> >>>  x
x
set()

set()

>>> >>>  x x = = {}
{}
>>> >>>  typetype (( xx )
)
<class 'dict'>
<class 'dict'>

An empty set is falsy in Boolean context:

一个空集在布尔上下文中是虚假的:

You might think the most intuitive sets would contain similar objects—for example, even numbers or surnames:

您可能会认为最直观的集合将包含相似的对象,例如,甚至数字或姓氏:

 >>> >>>  s1 s1 = = {
     {
      22 , , 44 , , 66 , , 88 , , 1010 }
}
>>> >>>  s2 s2 = = {
     {
      'Smith''Smith' , , 'McArthur''McArthur' , , 'Wilson''Wilson' , , 'Johansson''Johansson' }
}

Python does not require this, though. The elements in a set can be objects of different types:

但是,Python不需要此。 集合中的元素可以是不同类型的对象:

Don’t forget that set elements must be immutable. For example, a tuple may be included in a set:

不要忘记set元素必须是不变的。 例如,一个元组可以包含在集合中:

 >>> >>>  x x = = {
     {
      4242 , , 'foo''foo' , , (( 11 , , 22 , , 33 ), ), 3.141593.14159 }
}
>>> >>>  x
x
{42, 'foo', 3.14159, (1, 2, 3)}
{42, 'foo', 3.14159, (1, 2, 3)}

But lists and dictionaries are mutable, so they can’t be set elements:

但是列表和字典是可变的,因此不能将它们设置为元素:

设置大小和成员资格 (Set Size and Membership)

The len() function returns the number of elements in a set, and the in and not in operators can be used to test for membership:

len()函数返回集合中元素的数量, innot in运算符可用于测试成员资格:

 >>> >>>  x x = = {
     {
      'foo''foo' , , 'bar''bar' , , 'baz''baz' }

}

>>> >>>  lenlen (( xx )
)
3

3

>>> >>>  'bar' 'bar' in in x
x
True
True
>>> >>>  'qux' 'qux' in in x
x
False
False

在集合上操作 (Operating on a Set)

Many of the operations that can be used for Python’s other composite data types don’t make sense for sets. For example, sets can’t be indexed or sliced. However, Python provides a whole host of operations on set objects that generally mimic the operations that are defined for mathematical sets.

可用于Python其他复合数据类型的许多操作对于集合没有意义。 例如,集不能被索引或切片。 但是,Python在集合对象上提供了很多操作,这些对象通常模仿为数学集合定义的操作

运算符与方法 (Operators vs. Methods)

Most, though not quite all, set operations in Python can be performed in two different ways: by operator or by method. Let’s take a look at how these operators and methods work, using set union as an example.

Python中的大多数(虽然不是全部)设置操作可以通过两种不同的方式执行:通过运算符或方法。 让我们以集合联合为例,看看这些运算符和方法如何工作。

Given two sets, x1 and x2, the union of x1 and x2 is a set consisting of all elements in either set.

给定两个集合x1x2x1x2并集是一个包含两个集合中所有元素的集合。

Consider these two sets:

考虑以下两个集合:

The union of x1 and x2 is {'foo', 'bar', 'baz', 'qux', 'quux'}.

x1x2并集是{'foo', 'bar', 'baz', 'qux', 'quux'}

Note: Notice that the element 'baz', which appears in both x1 and x2, appears only once in the union. Sets never contain duplicate values.

注意:请注意,同时出现在x1x2中的元素'baz'在联合中仅出现一次。 集永远不会包含重复值。

In Python, set union can be performed with the | operator:

在Python中,可以使用|执行联合| 操作员:

 >>> >>>  x1 x1 = = {
     {
      'foo''foo' ,</
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值