What is a collection? python3 commonly used methods are there?

One: What is a collection?

1, a collection (set) of the container is variable.

2, within the set of data objects is only (not duplicated).

3, structural disorder of the collection is stored, data collection has no relationship (characteristics: insert, delete faster).

4, the elements in the set must be immutable.

5, the set is iterative.

6, only the key set is not equivalent to the value of the dictionary (the key data set).

Two: Create a collection of

1, creates an empty set: s = set ()

2, create a non-empty set: three integer s = {1,2,3} # set: 1,2, 3

3, a set of constructors:

Set () to create an empty set

Set (iterable) to create a new collection with iterables

Example:

s = set(‘ABC’)

s = set ( 'ABCCBA') ---- a plurality of A and C, the excess will default A \ C Delete

s = set ({1: 'a', 2: 'two', 3: 'three'}) with a set of dictionary generation, only the key, no value

s = set([1, 3.14, False])

s = set ((2,3,5,7)) ----- converted into a set of tuples

Three: a set of operations: intersection, union, complement, subset, superset

1, generates intersection of two sets: &

S1 = {1,2,3}

S2 = {2,3,4}

S3 = s1 & s2

2, and generates a set of two sets: | (i.e., in the presence of s1, s2 there is, the remove duplicates)

S1 = {1,2,3}

S2 = {2,3,4}

S3 = s1 | s2

4, generates two complement of a set: - generating a set belonging but s1, s2 elements are not all the elements.

S1 = {1,2,3}

S2 = {2,3,4}

S3 = s1 - s2

5, generates two sets of symmetrical complement: ^

S1 = {1,2,3}

S2 = {2,3,4}

S3 = s1 ^ s2

6, a determined set is a subset of another set: <

S1 = {1, 2, 3}

S2 = {2, 3}

S2 <s1 #True determined subset

7, a determined set is a superset of another set:>

S1 = {1, 2, 3}

S2 = {2, 3}

S1> s2 #True Analyzing superset

8, ==! = The same set of / different

S1 = {1, 2, 3}

S2 = {2, 3,1}

S1 == s2 #True

S1! = S2 # set of data has no relationship

9, in / not in operator

等同于字典,in运算符用于集合中,当某个值存在于集合中,则为真,否则为假,not in与in返回值相反。

示例:

S = {1, ‘Two’3.14}

1 in s #True

2 in s #False

3.14 not in s #False

4not in s #True

四:集合的方法:集合是可变对象,可用方法添加、删除集合的数据。

A:集合与列表等一些常用的方法相似

1、s.add(e) 在集合中添加一个新的元素e;如果元素已经存在,则不添加

2、s.remove(e) 从集合中删除一个元素,如果元素不存于集合中,则会产生一个KeyError

3、s.clear(e) 清空集合内的所有元素

4、s.discard(e) 从集合s中移除一个元素e,在元素e不存在时什么都不做

5、s.copy() 将集合进行一次浅拷贝

6、s.pop() 从集合中删除一个随机元素,如果此集合为空,则引发错误

7、update(s2) 用s与s2得到的全集更新

B:集合专用方法

1、s.difference(s2) 用s – s2运算,返回存在于在s中,但不在s2中的所有元素

2、s.difference_update(s2) 等同于s = s – s2

3、s.intersection(s2) 等同于s & s2

4、s.intersection_update(s2) 等现于 s = s &s2

5、s.isdisjoint(s2) 如果s与s2交集为空返回True,非空则返回False

6、s.issubset(s2) 如果s与s2交集为非空返回True,空则返回False

7、s.issuperset() 如果s 为s2的子集返回True,否则返回False

8、s.symmetric_difference(s2) 返回对称补集,等同于s^s2

9、s.symmetric_difference_update(s2) 用s与s2的对称补集更新s

10、s.union(s2) 生成s与s2的全集

五:练习

任意输入一个单词,存入集合中,当输入回车时,结束输入

1、打印您输入的单词的种类数(去重)

2、每个字母都打印到终端上显示


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

结语

今天要记的东西有点多,兄弟们,多多加油哦!如果喜欢小编的内容请互粉、评论、关注!

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

Guess you like

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