(一)元组

(1)元组的定义

元组与列表相似,不过元组的元素为只读的,不能修改。
创建元组,可以使用如下格式:

  1. #定义一个包含四个元素的元组
  2. tup1 = ("do","it", "right", "now")
  3. #定义一个包含数据库节点地址,端口号,用户名,密码的元组
  4. mysql_connection = ("localhost", "3306", "root", "123456")
  5. #定义一个空的元组
  6. tup2 = ()
  7. #定义一个包含一个元素的元组,需要在元素后面添加逗号,否则括号会被当成运算符,引起混淆
  8. tup3 = (12,)
  9. tup4 = ("be mercy",)

(2) 访问元组

格式: 元组名称[下标索引]

(3)元组的运算

元组运算方式基本与列表相同,但是不能对元组的单个元素进行删除或者修改,元组是只读的,只能对元组整体进行删除

运算符 说明
+ 连接两个元组
* 重复多次输出元组的内容
del() 删除元组
len() 测试元组的长度
max() 返回元组中元素最大值
min() 返回元组中元素最小值
tuple() 将列表转换为元组
in 判断元素在元组中否

(4)举例

  1. tu1=("go", "forward")
  2. tu2=("that's ", "it")
  3. #合并元组tu1和tu2,并合并
  4. tu3=tu1 + tu2
  5. print("tu3={}".format(tu3))
  6. #将元组tu3扩展三倍
  7. print("tu3={}".format(tu3*3))
  8. print(len(tu3))
  9. tu4={1,8,290,11,15}
  10. #打印tu4元组的最大值
  11. print(max(tu4))
  12. #打印tu4元组的最小值
  13. print("minimum={:d}".format(min(tu4)))
  14. #遍历tu3元组
  15. for x in tu3: print(x)

运行结果为:

  1. tu3=('go', 'forward', "that's ", 'it')
  2. tu3=('go', 'forward', "that's ", 'it', 'go', 'forward', "that's ", 'it', 'go', 'forward', "that's ", 'it')
  3. 4
  4. 290
  5. minimum=1
  6. go
  7. forward
  8. that's
  9. it

(二)字典

(1)关于字典及其定义

字典是一组key-value键值对,类似于jason格式的数据。

1) 定义一个字典可以使用以下格式:

  1. dict1 = {'Name':'Susan'}
  2. dict2 = {"stu_no":"17080021","name":"John"}
  3. #定义一个空字典
  4. dict3 = {}

2) 使用dict.copy()方法进行浅复制

所谓的浅复制,实质上是对原有变量的引用,复制以后的变量类似于:

  • windows中的快捷方式
  • linux中的软链接
  • c语言中的指向变量地址的引用

浅复制实际上是对被复制的对象的一种别名引用,因而当被复制对象发生了改变之后,浅复制生成对象同样发生了该表

在python中,对列表、字典变量的直接赋值,都属于浅复制。
而dict.copy()方法对第一层对象来说,是深复制;而第二层对象,同样是浅复制
例如:

  1. dict1 = {"location": {"state": "Massachussets", "city": "Cambridge"},
  2. "address": "No. 77, Massachessets Avenue", "zipcode": "02139"}
  3. #dict2为dict1的浅复制
  4. dict2 = dict1
  5. #dict3对于dict1来说,第一层是深复制,第二层是浅复制
  6. dict3 = dict1.copy()
  7. #修改zipcode
  8. dict1["zipcode"] = "02139-4307"
  9. #在location的成员字典中增加一个nation
  10. dict1["location"]["nation"]="The United States of American"
  11. #修改locaiton中的city的值
  12. dict1["location"]["city"]="Cambridge city"
  13. #增加一个tel键
  14. dict1["Tel"]="617-253-1000"
  15. print("dict1={}".format(dict1))
  16. print("dict2={}".format(dict2))
  17. print("dict3={}".format(dict3))

运行后,结果为:

  1. dict1={'location': {'state': 'Massachussets', 'city': 'Cambridge city', 'nation': 'The United States of American'}, 'address': 'No. 77, Massachessets Avenue', 'zipcode': '02139-4307', 'Tel': '617-253-1000'}
  2. dict2={'location': {'state': 'Massachussets', 'city': 'Cambridge city', 'nation': 'The United States of American'}, 'address': 'No. 77, Massachessets Avenue', 'zipcode': '02139-4307', 'Tel': '617-253-1000'}
  3. dict3={'location': {'state': 'Massachussets', 'city': 'Cambridge city', 'nation': 'The United States of American'}, 'address': 'No. 77, Massachessets Avenue', 'zipcode': '02139'}

dict2是dict1的浅复制,所以dict1发生改变,则dict2同样发生改变
dict3是dict1的copy复制,第一层深复制,第二层浅复制,则对dict1的第一层的该表,如:zipcode还有Tel,都没有复制到dict3,但是dict2同样发生了改变。
而location的值是一个字典,属于第二层,则对location的值的该表,同样传导到了dict2和dict3,因为对于第二层而言,无论是变量直接赋值,还是用copy()方法,都属于浅复制。

(2)字典值的访问,字典中键、值以及键值对的取出

1)字典中值的访问

键放入方括号内,可以实现对字典中值得访问,例如:

  1. dict1 = {"index":1, "buck":12, "title":"Management Science", "author":"Smith"}
  2. print("the titile of dict1 is:{}".format(dict1["title"]))

运行结果为:

  1. The titile of dict1 is:Management Science

也可以使用get方法,来实现对键值的访问,例如:

  1. dict1 = {"index":1, "buck":12, "title":"Management Science", "author":"Smith"}
  2. #指定获取buck键对应的值
  3. s1 = dict1.get("buck")
  4. #如果指定的Tel键不存在,则设置返回值为10086
  5. s2=dict1.get("Tel","10086")
  6. print(s1,s2)

结果为

  1. 12 10086

2)字典中键、值以及键值对的取出

  • dict.keys()方法可以取出一个字典变量的所有键
  • dict.values()方法可以取出一个字典变量的所有值
  • dict.items()方法可以取出每一个key-value键值对,并把每一个键值对放到一个元组中
    例如:
  1. dict1 = {"index":1, "buck":12, "title":"Management Science", "author":"Smith"}
  2. #取出键
  3. i = 0
  4. for key in dict1.keys():
  5. i +=1
  6. print("{:d}.key={}".format(i,key))
  7. #取出值
  8. j=0
  9. for value in dict1.values():
  10. j += 1
  11. print("{:d}.value=\"{}\"".format(j,value))
  12. k=0
  13. #取出键值对
  14. for item in dict1.items():
  15. k += 1
  16. print("Item {:d} is: {}".format(k,item))

运行结果为:

  1. 1.key=index
  2. 2.key=buck
  3. 3.key=title
  4. 4.key=author
  5. 1.value="1"
  6. 2.value="12"
  7. 3.value="Management Science"
  8. 4.value="Smith"
  9. Item 1 is: ('index', 1)
  10. Item 2 is: ('buck', 12)
  11. Item 3 is: ('title', 'Management Science')
  12. Item 4 is: ('author', 'Smith')

由于items方法将结果以元组的形式展现,因而我们也可以通过并列赋值的方式,将key和value分别赋给单独的变量,例如:

  1. dict1 = {"index":1, "buck":12, "title":"Management Science", "author":"Smith"}
  2. #将键值对的值分别赋给key和value变量
  3. for key,value in dict1.items():
  4. print("key={},value={}".format(key,value))

运行结果为:

  1. key=index,value=1
  2. key=buck,value=12
  3. key=title,value=Management Science
  4. key=author,value=Smith

3)dict.setdefault(key,default)方法

setdefault方法同样可以获取一个key对应的值,但是setdefault方法在获取key对应值的同时,还有设置功能,如果这个key在字典中不存在,则可以设置到字典中
例如:

  1. dict1={"grade":"17","class":"MS1701","Name":"Susan"}
  2. #获得name对应的值
  3. get1=dict1.setdefault("Name")
  4. print("get1=%s" % get1)
  5. #sex这个键并不存在,可以自动加入到字典中,并指定默认值为Female
  6. get2 = dict1.setdefault("Sex","Female")
  7. print("get2=%s" % get2)
  8. print("dict1={}".format(dict1))

运行结果为:

  1. get1=Susan
  2. get2=Female
  3. dict1={'grade': '17', 'class': 'MS1701', 'Name': 'Susan', 'Sex': 'Female'}

(3)增加键值对、修改和删除已有键对应的值

1)增加键值对,仅仅需要直接定新增键和值的定义即可

例如:

  1. dict1={"index":1, "buck":12, "title":"Management Science", "author":"Smith"}
  2. #新增一个school键
  3. dict1["school"]="Harvard University"
  4. print("dict1={}".format(dict1))

运行结果为:

  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'Harvard University'}

2) 修改已有的键值对

i) 只需对之重新赋值,就可以修改已经存在的键对应的值。

例如:

  1. dict1={"index":1, "buck":12, "title":"Management Science", "author":"Smith"}
  2. #新增一个school键
  3. dict1["school"]="Harvard University"
  4. #对school键对应的值进行修改
  5. dict1["school"] = "California Institute of Technology"
  6. #打印结果,可得
  7. print("dict1={}".format(dict1))

运行以后,可得

  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}

ii) dict.update(dict2)方法

可以把dict2的键值对更新到dict中,如果dict2中的键已经在dict中存在,则是更新;否则,是增加。例如:

  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
  2. dict2={"school":"Massachussets Institue of Technology ", "college":"Management Science and Engineering"}
  3. #用dict2更新dict1,对于已经在dict1中存在的school键,是替换;对于在dict1中不存在的college键,则是增加
  4. dict1.update(dict2)
  5. print("dict1={}".format(dict1))

运行结果为:

  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'Massachussets Institue of Technology ', 'college': 'Management Science and Engineering'}

3) 删除已有的键值对

i)del(),删除已有的键值对,例如
  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
  2. print("dict1={}".format(dict1))
  3. # 删除buck键对应的值
  4. del dict1['buck']
  5. print("dict1={}".format(dict1))

运行结果为

  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
  2. dict1={'index': 1, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}

ii)dict.clear()方法,将整个字典内容清空
  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
  2. print("dict1={}".format(dict1))
  3. #置空dict1字典
  4. dict1.clear()
  5. #现在打印出来的dict1只是一个空字典了
  6. print("dict1={}".format(dict1))

运行结果为:

  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
  2. dict1={}

iii)dict.pop(key)方法,删除给定键key对应的值,返回值为被删除的值,key值必须给出。否则,返回default值。

例如:

  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
  2. print("dict1={}".format(dict1))
  3. dict1.pop("buck")
  4. print("dict1={}".format(dict1))

运行结果为:

  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
  2. dict1={'index': 1, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}

iv) dict.popitem()方法,随机返回并删除字典中的一对键和值(一般删除末尾一对键值)
  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
  2. print("dict1={}".format(dict1))
  3. #删除了末尾的一对键值
  4. dict1.popitem()
  5. print("dict1={}".format(dict1))

运行结果为:

  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
  2. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith'}

(4)字典长度和对字符串的转换

  • len()字典长度
  • str(dict)将字典转换为字符串
    例如:
  1. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith'}
  2. dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith'}
  3. print("the length of dict1 is:%d" % len(dict1))
  4. print(str(dict1))

运行结果为:

  1. the length of dict1 is:4
  2. {'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith'}

(5)从元组和列表转换为字典

fromkeys()方法可以将一个序列转换为字典,序列中的各个值为字典中的键,而如果指定了默认值,则字典中的各个键值为默认值,否则为None
举例如下:

  1. # 初始化一个元祖
  2. seq1=("1","3","note","that")
  3. #初始化一个字典
  4. dict1={}
  5. #注意:fromkeys方法本身返回一个新的字典,但是不改变dict1的值。要将返回的新的字典赋给一个变量保存
  6. #1为default值
  7. dict1 = dict1.fromkeys(seq1, 1)
  8. print("dict1=%s" % dict1)
  9. seq2=("2","4","do","it")
  10. #fromkey方法对原有的字典的内容不保存,使用指定的序列生成一个新的字典,完全替换掉原有内容,并返回
  11. dict1 = dict1.fromkeys(seq2, 2)
  12. print("dict1=%s" % dict1)
  13. #也可以从列表生成
  14. list3=["Name","No","grade","class"]
  15. dict1 = dict1.fromkeys(list3,3)
  16. print("dict1=%s" % dict1)

运行结果如下:

  1. dict1={'1': 1, '3': 1, 'note': 1, 'that': 1}
  2. dict1={'2': 2, '4': 2, 'do': 2, 'it': 2}
  3. dict1={'Name': 3, 'No': 3, 'grade': 3, 'class': 3}

(三)集合

(1)集合的创建

集合是一个无序不重复元素的集。基本功能包括关系测试和消除重复元素。

创建集合,可以使用大括号括起来的元素列表,或者用set(),但是请注意:如果要创建空的集合,必须使用set(),而不能用{},因为这会导致创建一个空字典,而不是空集合。
以下为几个示例:

  1. list1 = ["Life", "was", "like", "a", "box", "of", "chocolates,",
  2. "you", "never", "know", "what", "you're", "gonna", "get"]
  3. tu1 = (1,8,0,67,12,0,8,11,8,1)
  4. #直接定义集合,定义中的重复元素会被自动去掉
  5. set1 = {"Today", "I", "consider", "myself",
  6. "the", "luckiest", "man", "on", "the", "face", "of", "the", "Earth"}
  7. #由元组转化,重复元素去掉
  8. set2 = set(tu1)
  9. #由列表转化,重复元素去掉
  10. set3 = set(list1)
  11. #通过打印出来的结果,可以看到,集合是无序的
  12. print("set1=%s" % set1)
  13. print("set2={}".format(set2))
  14. print("set3={}".format(set3))
  15. #创建一个空的集合
  16. set4 = set()

列表、元组中的元素值可以用下标获取,字典中的值可以用键获取,但是集合中的值不能用下标获取。
可以考虑将集合转化为列表或者元组之后,再设法取得,转化例子如下:

  1. list1 = ["Life", "was", "like", "a", "box", "of", "chocolates,",
  2. "you", "never", "know", "what", "you're", "gonna", "get"]
  3. tu1 = (1,8,0,67,12,0,8,11,8,1)
  4. set2 = set(tu1)
  5. set3 = set(list1)
  6. #将set2转化为列表
  7. list2 = list(set2)
  8. print("list2=%s" % list2)
  9. #将set3转化为元组
  10. tu2 = tuple(set3)
  11. print("tuple2={}".format(tu2))

运行结果为:

  1. list2=[0, 1, 67, 8, 11, 12]
  2. tuple2=('like', 'box', 'you', 'was', 'chocolates,', 'know', 'Life', "you're", 'never', 'of', 'get', 'a', 'what', 'gonna')

(2)集合的增删查

1)判断元素是否在集合中

同列表、元组和字典一样,在时候用集合的时候,我们同样可以用in来判断元素是否在集合中
例如:

  1. set1 = {"Today", "I", "consider", "myself",
  2. "the", "luckiest", "man", "on", "the", "face", "of", "the", "Earth"}
  3. # in判断set1中是否有元素"Today"
  4. if "Today" in set1:
  5. print("True")
  6. else:
  7. print("False")

运行结果为:

  1. True

类似于列表、元组与字典,使用in关键字同样也可以对set进行遍历,例如:

  1. et1 = {"Today", "I", "consider", "myself",
  2. "the", "luckiest", "man", "on", "the", "face", "of", "the", "Earth"}
  3. j = 0
  4. for item in set1:
  5. j += 1
  6. print("item%d=%s" % (j,item))

运行结果为:

  1. item1=consider
  2. item2=Today
  3. item3=on
  4. item4=the
  5. item5=myself
  6. item6=face
  7. item7=Earth
  8. item8=luckiest
  9. item9=man
  10. item10=I
  11. item11=of

2)add()方法添加元素到集合

由于列表是有序的,因而在列表中有append、extend以及insert等添加元素的方法,但是set是无序的,因而在set中添加元素只有add和update方法。

  1. set1 = {"Today", "I", "consider", "myself",
  2. "the", "luckiest", "man", "on", "the", "face", "of", "the", "Earth"}
  3. # 注意:add方法的参数不可以是列表
  4. #set1.add(["do", "it"])
  5. # add方法的参数也不可以是字典
  6. #set1.add({"Number":15})
  7. #add方法的参数可以是元组
  8. set1.add(("do", "it"))
  9. #add可以多次添加
  10. set1.add("Not")
  11. print("set1={}".format(set1))

运行结果为:

  1. set1={'Not', 'on', 'myself', 'the', 'I', 'Earth', 'consider', 'man', 'luckiest', 'Today', 'of', ('do', 'it'), 'face'}

3)update()方法添加元素

update方法是一个相当全能的方法,对字典、字符串、列表和元组都可以处理,其特点如下:

  • update可以实现叠加操作,每次update都会对原集合进行处理
  • 如果update的操作对象是元组,则元组的每个元素独立加入set,成为set的一个新元素
  • 如果update的操作对象是字符串,则字符串中的每个字符独立加入set,成为set的一个新元素
  • 如果update的操作对象是列表,则列表中的每一项都独立加入set,成为set的一个新元素
  • 如果update的操作对象是字典,则字典中的每一个键都会独立加入set,成为set的一个新元素,而值被舍弃
    例如:
  1. set1 = {"Today", "I", "consider", "myself",
  2. "the", "luckiest", "man", "on", "the", "face", "of", "the", "Earth"}
  3. tu1=("I", "am", "a", "monster", "on", "the", "hill")
  4. list1=["the", "luckiest", "one"]
  5. #元组中每个元素独立加入
  6. set1.update(tu1)
  7. print("set1={}".format(set1))
  8. #列表中每个元素独立加入
  9. set1.update(list1)
  10. print("set1={}".format(set1))
  11. #字符串中每个字符独立加入
  12. str1 = "No more than five years"
  13. set1.update(str1)
  14. print("set1={}".format(set1))
  15. #字典中每个键独立加入,值舍去
  16. dict1 = {"Number": 123, "Name": "zs"}
  17. set1.update(dict1)
  18. print("set1={}".format(set1))

结果为:

  1. set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'the', 'Today', 'am', 'man'}
  2. set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}
  3. set1={'r', 'N', 'of', 'one', 'v', ' ', 'myself', 'I', 'hill', 'e', 'the', 'Today', 'm', 't', 'man', 's', 'o', 'on', 'monster', 'luckiest', 'Earth', 'h', 'i', 'n', 'f', 'consider', 'face', 'y', 'am', 'a'}
  4. set1={'Name', 'r', 'N', 'of', 'one', 'v', ' ', 'myself', 'I', 'hill', 'e', 'the', 'Today', 'm', 't', 'man', 's', 'o', 'on', 'monster', 'luckiest', 'Earth', 'h', 'i', 'n', 'f', 'consider', 'face', 'y', 'am', 'a', 'Number'}

从结果中,我们也可以发现集合的两个重要特点

  • 1.无序
  • 2.去重

4)remove()方法,移除元素

remove()方法允许通过指定元素值的方式,将元素从集合中移除。例如:

  1. set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}
  2. print("set1={}".format(set1))
  3. set1.remove('face')
  4. print("set1={}".format(set1))

运行结果为:

  1. set1={'one', 'I', 'of', 'luckiest', 'on', 'myself', 'face', 'the', 'monster', 'man', 'am', 'Today', 'Earth', 'a', 'hill', 'consider'}
  2. set1={'one', 'I', 'of', 'luckiest', 'on', 'myself', 'the', 'monster', 'man', 'am', 'Today', 'Earth', 'a', 'hill', 'consider'}

如果被移除的值不存在,则会发生异常,例如:

  1. set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}
  2. print("set1={}".format(set1))
  3. set1.remove('me')
  4. print("set1={}".format(set1))

运行结果为:

  1. Traceback (most recent call last):
  2. File "C:/Users/thomas/PycharmProjects/py_v3/Lesson3_exercise.py", line 113, in <module>
  3. set1.remove('me')
  4. KeyError: 'me'

5)discard()方法移除元素

discard方法也允许从集合中移除元素,而且在指定的元素不存在的时候不会报错。例如:

  1. set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}
  2. print("set1={}".format(set1))
  3. set1.discard('consider')
  4. print("set1={}".format(set1))

运行结果为:

  1. set1={'monster', 'of', 'am', 'on', 'one', 'the', 'man', 'face', 'Today', 'hill', 'I', 'a', 'consider', 'luckiest', 'myself', 'Earth'}
  2. set1={'monster', 'of', 'am', 'on', 'one', 'the', 'man', 'face', 'Today', 'hill', 'I', 'a', 'luckiest', 'myself', 'Earth'}

如果是不存在的元素,则有:

  1. set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}
  2. print("set1={}".format(set1))
  3. set1.discard('me')
  4. print("set1={}".format(set1))

运行结果为:

  1. set1={'luckiest', 'the', 'man', 'of', 'consider', 'I', 'Earth', 'face', 'a', 'on', 'myself', 'hill', 'one', 'Today', 'monster', 'am'}
  2. set1={'luckiest', 'the', 'man', 'of', 'consider', 'I', 'Earth', 'face', 'a', 'on', 'myself', 'hill', 'one', 'Today', 'monster', 'am'}

可见,当使用discard之后,如果指定了一个不存在的元素,discard方法就什么都不做,也不报错。

6)pop()方法随机删除元素

pop方法允许从集合中随机删除一个元素,例如:

  1. set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}
  2. print("set1={}".format(set1))
  3. set1.pop()
  4. print("set1={}".format(set1))

运行结果为:

  1. set1={'one', 'on', 'Earth', 'am', 'myself', 'consider', 'I', 'the', 'a', 'hill', 'monster', 'face', 'man', 'luckiest', 'Today', 'of'}
  2. set1={'on', 'Earth', 'am', 'myself', 'consider', 'I', 'the', 'a', 'hill', 'monster', 'face', 'man', 'luckiest', 'Today', 'of'}

(3)集合的并交差运算

集合可以进行并交叉等各种运算,如:

  1. set_a = {'cucumber', 'cabbage', 'tomato'}
  2. set_b = {'apple', 'banana', 'tomato'}
  3. #并集运算,取两个集合之和
  4. set_union = set_a | set_b
  5. print("set_union={}".format(set_union))
  6. #交集运算,去两个集合的交集
  7. set_intersection = set_a & set_b
  8. print("set_intersection={}".format(set_intersection))
  9. #差集运算,取在第一个集合中存在,但是第二个集合中不存在的元素
  10. set_difference = set_a - set_b
  11. print("set_difference={}".format(set_difference))
  12. #对称差集运算,取在两个集合中存在,但不同时存在的元素
  13. set_symmetric_difference = set_a ^ set_b
  14. print("set_symmetric_difference={}".format(set_symmetric_difference))
  15. #并集运算
  16. set_union = set_a.union(set_b)
  17. print("set_union={}".format(set_union))
  18. #交集运算
  19. set_intersection = set_a.intersection(set_b)
  20. print("set_intersection={}".format(set_intersection))
  21. #差集运算
  22. set_difference = set_a.difference(set_b)
  23. print("set_difference={}".format(set_difference))
  24. #对称差集运算
  25. set_symmetric_difference = set_a.symmetric_difference(set_b)
  26. print("set_symmetric_difference={}".format(set_symmetric_difference))

运算结果为:

  1. set_union={'cabbage', 'apple', 'cucumber', 'tomato', 'banana'}
  2. set_intersection={'tomato'}
  3. set_difference={'cabbage', 'cucumber'}
  4. set_symmetric_difference={'cabbage', 'cucumber', 'apple', 'banana'}
  5. set_union={'cabbage', 'apple', 'cucumber', 'tomato', 'banana'}
  6. set_intersection={'tomato'}
  7. set_difference={'cabbage', 'cucumber'}
  8. set_symmetric_difference={'cabbage', 'cucumber', 'apple', 'banana'}

(四)字符串函数

Python内建的字符串处理函数非常丰富,为字符串处理提供了有力的工具。
这里试列举一些字符串处理函数

名称 描述
capitalize() 将字符串的第一个字符转换为大写
center(width,fillchar) 返回一个指定宽度为width居中的字符,fillchar为填充的字符,默认为空格
count(str, beg=0, end=len(string)) 返回str在string中出现的次数,如果beg或者end指定,则返回指定范围内出现str的次数
encode(encoding=’UTF-8’,errors=”strict”) 以encoding指定的编码格式编码字符串,如果出错默认报一个ValueError的异常,除非erros指定的是’ignore’或者’replace’
bytes.decode(encoding=”utf-8”,errors=”strict”) python3中的字符串没有decode方法,但是由encode方法返回的bytes对象有decode方法,可以按指定编码格式解码
endswith(suffix,beg=0,end=len(string)) 检查字符串是否以obj结束,如果beg或者end指定则检查指定的范围内是否以obj结束,如果是,返回True,否则返回False
startswith(suffix, beg=0, end=len(string) 检查字符串是否以obj开头,是则返回True,否则返回False。如果beg和end指定值,则在指定范围内检查
expandtabs(tabsize=8) 把字符串string中的tab符号转换为空格,tab符号默认的空格数为8
find(str,beg=0,end=len(string)) 检测str是否包含在字符串中,如果指定范围beg和end,则检查是否包含在指定范围内,如果包含则返回开始的索引值,否则返回-1
index(str, beg=0, end=len(string)) 同find方法一样,不过如果str不在字符串中会报一个异常
join(seq) 以指定字符串为分隔符,将seq中所有的元素(的字符串表示)合并为一个新的字符串
ljust(width[,fillchar]) 返回一个元字符串左对齐,并用fillchar填充至长度width的新字符串, fillchar默认为空格
lower() 转换字符串中所有大写字符为小写
upper() 转换字符串中所有小写字符为大写
split(str=””,num=string.count(str)) 以str为分隔符截取字符串,如果num有指定值,则仅截取num个子字符串
isalnum() 如果字符串中至少有一个字符,并且所有字符都是字母或数字,则返回True,否则返回False。这在对输入进行检查的时候非常有用
isalpha() 如果字符串至少有一个字符,并且有所的字符都是字母,则返回True,否则返回False
isdigit() 如果字符串至少有一个字符,并且所有的字符都是数字,则返回True,否则返回False
isnumeric() 如果字符串中至少包含一个字符,并且所有字符都是数字,则返回True,否则返回False。
isdecimal() 检查字符串中是否只包含十进制字符,这种方法只存在于unicode对象
islower() 如果字符串中包含至少一个区分大小写的字符,并且所有这些区分大小写的字符都是小写,则返回True,否则返回False
isspace() 如果字符串中只包含空白,则返回True,否则返回False
isupper() 如果字符串中至少至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回True,否则返回False
istitle() 如果字符串是标题化的,则返回True,否则返回False

举例如下:

  1. # 字符串第一个字母大写
  2. s="jimmy doolittle"; print("s=%s" % s.capitalize());
  3. # 字符串置中
  4. print("%s" % s.center(80, "#"))
  5. # 统计字符串中指定字符的个数
  6. print("count of mm:%d" % s.count("mm"))
  7. print("count of l:%d" % s.count("l"))
  8. #指定起始位置和终止位置
  9. print("count of m:%d" % s.count("m", 2))
  10. print("count of m:%d" % s.count("m", 3))
  11. s_example = '我们爱学习'
  12. #指定字符编码格式,返回bytes对象
  13. s_gbk = s_example.encode('GBK')
  14. s_utf8 = s_example.encode('UTF-8')
  15. print("GBK:", s_gbk)
  16. print("UTF-8:", s_utf8)
  17. #对bytes对象按指定格式解码
  18. print(s_utf8.decode('UTF-8'))
  19. print(s_gbk.decode('gbk'))
  20. #判断特定字符串是否在起始位置以及终止位置,或者指定范围的起始位置以及终止位置
  21. s_example = '我们爱学习,我们爱科学,我们爱劳动,我们爱学校, 我们爱老师'
  22. print("endswith 老师", s_example.endswith('老师', 10))
  23. print("endswith 科学", s_example.endswith('科学'))
  24. print("startwith 我们", s_example.startswith('我们'))
  25. print("startswith 我们 from the 3rd character", s_example.startswith('我们',2))
  26. print("startswith 我们 from the 6th character", s_example.startswith('我们',6))
  27. #将tab转为空格,默认一个tab八个空格
  28. s_tab = "Nothing's gonna change my love for you"
  29. s_tab.expandtabs()
  30. print("expandtabs=", s_tab)
  31. #查找指定字符串的位置,可以指定范围
  32. print("find '我们'", s_example.find("我们"))
  33. print("index of '爱'", s_example.find('爱'))
  34. print("find '我们' between index 3 and 11 is:", s_example.find("我们", 3, 11))
  35. #以给定字符为分隔符,合并seq中的字符串
  36. s1 = ","
  37. seq = ["This","is","a","good","point"]
  38. s_join1=s1.join(seq)
  39. print("s_join1_no_seperation=", s_join1)
  40. #分隔符为空,合并字符串
  41. s1 = ""
  42. seq = ("The", "Sun", "the", "Moon", "or", "the", "Earth")
  43. s_join = s1.join(seq)
  44. print("s_join=", s_join)
  45. #字符串左对齐,不满指定长度,则右边用指定字符填充
  46. s1 = "Hold on a second"
  47. s2=s1.ljust(30, "*")
  48. print("s_ljust:", s2)

运行结果为:

  1. s=Jimmy doolittle
  2. ################################jimmy doolittle#################################
  3. count of mm:1
  4. count of l:2
  5. count of m:2
  6. count of m:1
  7. GBK: b'\xce\xd2\xc3\xc7\xb0\xae\xd1\xa7\xcf\xb0'
  8. UTF-8: b'\xe6\x88\x91\xe4\xbb\xac\xe7\x88\xb1\xe5\xad\xa6\xe4\xb9\xa0'
  9. 我们爱学习
  10. 我们爱学习
  11. endswith 老师 True
  12. endswith 科学 False
  13. startwith 我们 True
  14. startswith 我们 from the 3rd character False
  15. startswith 我们 from the 6th character True
  16. expandtabs= Nothing's gonna change my love for you
  17. find '我们' 0
  18. index of '' 2
  19. find '我们' between index 3 and 11 is: 6
  20. s_join1_no_seperation= This,is,a,good,point
  21. s_join= TheSuntheMoonortheEarth
  22. s_ljust: Hold on a second**************

一个关于使用upper和lower的例子:

  1. confirmation = input("您想去结账吗(y/n)")
  2. #利用upper和lower方法对输入字符进行处理,保证无论用户输入的是大写还是小写,都不会有差别
  3. if confirmation.upper() == 'Y':
  4. print("Paying the bill...")
  5. elif confirmation.lower() == 'n':
  6. print("Go back for shopping")
  7. else:
  8. print("Wrong option")

运行结果为:

  1. 您想去结账吗(y/nN
  2. Go back for shopping

一个关于startswith和endswith的例子

  1. filelist = ['20180410.log', '20180310.log']
  2. # 根据文件起始名称和尾缀来判断是否是需要的文件,假设要统计的是2018年04月的log文件
  3. for filename in filelist:
  4. if filename.startswith('201804') and filename.endswith('log'):
  5. print("{} - that's what I want".format(filename))

运行结果为:

  1. 20180410.log - that's what I want

关于split的例子:

  1. single_line = '115.231.99.83 m.example.com [10/Apr/2018:17:58:12 +0800] "GET /404.html HTTP/1.1" "200" 1762 "-" "Apache-HttpClient/4.5.5 (Java/1.8.0_161)" 100.89.115.104 100.217.210.287:443 "-" "0.002" "0.002" "-" "200" "-"'
  2. list1=[]
  3. #使用split对指定字符串进行分割,默认分隔符为空格,分割为一个序列,可以赋给一个列表
  4. list1=single_line.split()
  5. print(list1)

结果为:

  1. ['115.231.99.83', 'm.example.com', '[10/Apr/2018:17:58:12', '+0800]', '"GET', '/404.html', 'HTTP/1.1"', '"200"', '1762', '"-"', '"Apache-HttpClient/4.5.5', '(Java/1.8.0_161)"', '100.89.115.104', '100.217.210.287:443', '"-"', '"0.002"', '"0.002"', '"-"', '"200"', '"-"']

(五)函数

(1)python3函数

函数是组织好的,可重复使用的,用来实现单一,或相关功能的代码段。
函数能提高应用的模块性,和代码的重复利用率。

(2)定义一个函数

定义函数遵循以下规则:

  • 函数代码块以def关键词开头,后接函数标识符名称和圆括号()
  • 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数
  • 函数内容以冒号起始,并且缩进
  • return[表达式]结束函数,选择性地返回一个值给调用方。不带表达式的retrun相当于返回None.

(3)语法

python定义函数使用def关键字,一般格式如下:

  1. def 函数名(参数列表):
  2. 函数体

(4)调用

函数参数,调用的时候形参和实参个数相同,参数顺序相同

例如:

  1. def x_y_sum_return(x,y):
  2. res = x + y
  3. return res
  4. #两个实参,返回一个计算结果
  5. z = x_y_sum_return(2,3)
  6. print(z)
  7. def x_y_comp_list(x,y):
  8. res1 = x + y
  9. res2 = x * y
  10. res_list = [res1,res2]
  11. return res_list
  12. #将计算结果放在一个列表中返回,返回一个参数
  13. print(x_y_comp_list(2,3))
  14. def x_y_comp_tuple(x,y):
  15. res1 = x + y
  16. res2 = x * y
  17. res_tuple = (res1,res2)
  18. return res_tuple
  19. #将计算结果放在一个元组中返回,返回一个参数
  20. tu1 = x_y_comp_tuple(100,299)
  21. print("tu1=",tu1)
  22. def x_y_comp_tuple2(x,y):
  23. res1 = x + y
  24. res2 = x * y
  25. return res1,res2
  26. #使用两个变量来接收元组返回的两个结果
  27. a,b = x_y_comp_tuple2(3,6)
  28. print("a=%d,b=%d" % (a,b))

运行结果为:

  1. 5
  2. [5, 6]
  3. tu1= (399, 29900)
  4. a=9,b=18