当前位置:   article > 正文

python入门之函数调用第三关函数与函数调用分清主次_第三章:Python基础の函数和文件操作实战...

第3关:函数与函数调用 - 分清主次

本課主題

Set 集合和操作实战

函数介紹和操作实战

参数的深入介绍和操作实战

format 函数操作实战

lambda 表达式介绍

文件操作函数介紹和操作实战

本周作业

Set 集合和操作实战

Set 是一个无序且不容许重复的序列

创建集合有两种方式:

第一种是通过直接创建、例如:s = {11,22}

ContractedBlock.gif

ExpandedBlockStart.gif

1 >>> se = {11,222,11,222}2 >>> print(se)3 {11, 222}4 >>>type(se)5

创建集合 方法一

第二种是通过转换。从一个可迭代类型的数组、元组转换成集合 Set。例如:s = Set(list)

ContractedBlock.gif

ExpandedBlockStart.gif

1 >>> li = [11,22,11,33,11,44,55]2 >>> se = set(li) #把列表转换成集合

3 >>>se4 {33, 11, 44, 22, 55}5 >>>type(se)6

创建集合 方法二

原理:在调用 Set 函数时,它会自动调用 __intit__ 方法,这叫构造方法 ,其实内部会执行一个 For 循环,循环这个"可迭代对象"里的所有元素,一个一个添加到集合里(什么是构造方法会在后期学到面向对象后再分享)

Set 集合的功能

这是Set 集合在 Python3.5.2的源码,我把它贴进来,里面有很多双下划线的方法,现在我都不知道是什么,让我了解后会回来补充一下。现在最主要的是要了解下面这几个集合的功能:我把它归类为添加、更新、找不同、找相同、删除和返回Boolean值六大类。

ContractedBlock.gif

ExpandedBlockStart.gif

classset(object):"""set() -> new empty set object

set(iterable) -> new set object

Build an unordered collection of unique elements."""

def add(self, *args, **kwargs): #real signature unknown

"""Add an element to a set.

This has no effect if the element is already present."""

pass

def clear(self, *args, **kwargs): #real signature unknown

"""Remove all elements from this set."""

pass

def copy(self, *args, **kwargs): #real signature unknown

"""Return a shallow copy of a set."""

pass

def difference(self, *args, **kwargs): #real signature unknown

"""Return the difference of two or more sets as a new set.

(i.e. all elements that are in this set but not the others.)"""

pass

def difference_update(self, *args, **kwargs): #real signature unknown

"""Remove all elements of another set from this set."""

pass

def discard(self, *args, **kwargs): #real signature unknown

"""Remove an element from a set if it is a member.

If the element is not a member, do nothing."""

pass

def intersection(self, *args, **kwargs): #real signature unknown

"""Return the intersection of two sets as a new set.

(i.e. all elements that are in both sets.)"""

pass

def intersection_update(self, *args, **kwargs): #real signature unknown

"""Update a set with the intersection of itself and another."""

pass

def isdisjoint(self, *args, **kwargs): #real signature unknown

"""Return True if two sets have a null intersection."""

pass

def issubset(self, *args, **kwargs): #real signature unknown

"""Report whether another set contains this set."""

pass

def issuperset(self, *args, **kwargs): #real signature unknown

"""Report whether this set contains another set."""

pass

def pop(self, *args, **kwargs): #real signature unknown

"""Remove and return an arbitrary set element.

Raises KeyError if the set is empty."""

pass

def remove(self, *args, **kwargs): #real signature unknown

"""Remove an element from a set; it must be a member.

If the element is not a member, raise a KeyError."""

pass

__hash__ =Nonedef symmetric_difference(self, *args, **kwargs): #real signature unknown

"""Return the symmetric difference of two sets as a new set.

(i.e. all elements that are in exactly one of the sets.)"""

pass

def symmetric_difference_update(self, *args, **kwargs): #real signature unknown

"""Update a set with the symmetric difference of itself and another."""

pass

def union(self, *args, **kwargs): #real signature unknown

"""Return the union of sets as a new set.

(i.e. all elements that are in either set.)"""

pass

def update(self, *args, **kwargs): #real signature unknown

"""Update a set with the union of itself and others."""

pass

def __and__(self, *args, **kwargs): #real signature unknown

"""Return self&value."""

pass

def __contains__(self, y): #real signature unknown; restored from __doc__

"""x.__contains__(y) <==> y in x."""

pass

def __eq__(self, *args, **kwargs): #real signature unknown

"""Return self==value."""

pass

def __getattribute__(self, *args, **kwargs): #real signature unknown

"""Return getattr(self, name)."""

pass

def __ge__(self, *args, **kwargs): #real signature unknown

"""Return self>=value."""

pass

def __gt__(self, *args, **kwargs): #real signature unknown

"""Return self>value."""

pass

def __iand__(self, *args, **kwargs): #real signature unknown

"""Return self&=value."""

pass

def __init__(self, seq=()): #known special case of set.__init__

"""set() -> new empty set object

set(iterable) -> new set object

Build an unordered collection of unique elements.

# (copied from class doc)"""

pass

def __ior__(self, *args, **kwargs): #real signature unknown

"""Return self|=value."""

pass

def __isub__(self, *args, **kwargs): #real signature unknown

"""Return self-=value."""

pass

def __iter__(self, *args, **kwargs): #real signature unknown

"""Implement iter(self)."""

pass

def __ixor__(self, *args, **kwargs): #real signature unknown

"""Return self^=value."""

pass

def __len__(self, *args, **kwargs): #real signature unknown

"""Return len(self)."""

pass

def __le__(self, *args, **kwargs): #real signature unknown

"""Return self<=value."""

pass

def __lt__(self, *args, **kwargs): #real signature unknown

"""Return self

pass@staticmethod#known case of __new__

def __new__(*args, **kwargs): #real signature unknown

"""Create and return a new object. See help(type) for accurate signature."""

pass

def __ne__(self, *args, **kwargs): #real signature unknown

"""Return self!=value."""

pass

def __or__(self, *args, **kwargs): #real signature unknown

"""Return self|value."""

pass

def __rand__(self, *args, **kwargs): #real signature unknown

"""Return value&self."""

pass

def __reduce__(self, *args, **kwargs): #real signature unknown

"""Return state information for pickling."""

pass

def __repr__(self, *args, **kwargs): #real signature unknown

"""Return repr(self)."""

pass

def __ror__(self, *args, **kwargs): #real signature unknown

"""Return value|self."""

pass

def __rsub__(self, *args, **kwargs): #real signature unknown

"""Return value-self."""

pass

def __rxor__(self, *args, **kwargs): #real signature unknown

"""Return value^self."""

pass

def __sizeof__(self): #real signature unknown; restored from __doc__

"""S.__sizeof__() -> size of S in memory, in bytes"""

pass

def __sub__(self, *args, **kwargs): #real signature unknown

"""Return self-value."""

pass

def __xor__(self, *args, **kwargs): #real signature unknown

"""Return self^value."""

pass

__hash__ = None

Set集合的源码

添加: add( ), unions( )

更新: difference_update( ), symmeteric_difference_update( ), intersection_update( ), update( )

找不同: difference( ), symmeteric_difference( ),

找相同: intersection( )

删除: discard( ), remove( ), clear( ), pop( )

返回Boolean值: isdisjoint( ), issuperset( )

添加:add()

ContractedBlock.gif

ExpandedBlockStart.gif

1 >>> se = set() #创建空集合

2 >>>se3 set()4 >>> se.add(123) #添加123到集合里

5 >>>se6 {123}

集合功能:添加

清除所有在集合里的内容:clear()

ContractedBlock.gif

ExpandedBlockStart.gif

1 >>> se = set() #创建一个空集合

2 >>> se.add(111)3 >>> se.add(222)4 >>> se.add(333)5 >>> print(se)6 {333, 222, 111}7 >>> se.clear() #清除集合里的所有元素

8 >>>se9 set()

集合功能:删除

对比两个集合,找其中一方的不同,相当于 database 里的 left outer join: difference()

1005794-20160828195418543-1190077685.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/366304?site
推荐阅读
相关标签
  

闽ICP备14008679号