What is the derivation python3 collection? A fixed set of what used to doing?

A: set of derivations

1, with iterables to create (generate) expression set is called the set of derivations

2, Grammar

Variable in the expression for {iterables [if truth expression]}

Note: [] brackets part may be omitted

Example:

L = [2,3,5,7,3,5,7,11]

S = {x**2 for x in L}

Analysis: First, the definition of a case table, for use inside the loop through the list of elements, then the x value is then passed to expression, which calculates the square element, and finally to remove duplicate data converted into a set of printed output. FIG code is as follows:

3, the derivation of a set of nested

Rules and list comprehensions same. Example:

S = ‘ABC’

n = ‘123’

s1 = {x + y for x in ‘ABC’ for y in ‘123’}

Analysis: two strings are defined and assigned to loop through the inside of the string value S, the first element of the S x expression pass, and then traversing n elements, the first element of y n pass. expression, expressions adding the elements, then y then passed to the second element of expression, be added to the first element of s. Until complete traversal element n, then S is traversing the second result value below ......

4. Exercise

Simulation of a student in class online system, known class list of students randomly print the student's name be named, and whether the student has been attending, enter 'y' representatives have to, on behalf of the other inputs are not present. Printing not yet reached the end of the roll call list.

Two: a fixed set (frozenset)

Set 1, a fixed set of immutable, disorder, contains a unique element

2, a fixed set of keys of a dictionary may also be used as a value (element) set

3, format:

Create a fixed set of empty:

fs = frozenset()

Create a non-empty set of fixed

Fs = frozenset([2,3,5,7])

Constructor:

Frozenset()

Frozenset (iterables) set with the same function, a fixed set of returns

4, the following arithmetic operation :( fixed rule set is equivalent to the usage of the set in)

& Intersection

| Union

-Complement

^ Symmetrical complement

<>= < <= == !=

In / not in

5, a fixed set of methods:

The method corresponds to the set of all sets of methods modified to remove

to sum up:

相比较其它语句,固定集合用的很少,我们一般用集合,这章节的内容很简单,大家只要记住集合的推导式就行。

推荐我们的Python学习扣qun:784758214 ,看看前辈们是如何学习的!从基础的python脚本到web开发、爬虫、django、数据挖掘等【PDF,实战源码】,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每天都有大牛定时讲解Python技术,分享一些学习的方法和需要注意的小细节,点击加入我们的 python学习者聚集地

发布了35 篇原创文章 · 获赞 4 · 访问量 3万+

Guess you like

Origin blog.csdn.net/ITHHH777/article/details/104210095