本节内容:

1、列表、元组操作
2、字符串操作
3、字典操作
4、集合操作
5、文件操作
6、字符编码与转码

4、集合操作

创建集合set

python set类是在python的sets中,大家现在使用的python2.7.x中,不需要导入sets模块可以直接创建集合。
>>>set('boy')
set(['y', 'b', 'o'])

集合添加、删除

python 集合的添加有两种常用方法,分别是add和update。
集合add方法:是把要传入的元素做为一个整个添加到集合中,例如:
>>> a = set('boy')
>>> a.add('python')
>>> a
set(['y', 'python', 'b', 'o'])


集合update方法:是把要传入的元素拆分,做为个体传入到集合中,例如:
>>> a = set('boy')
>>> a.update('python')
>>> a
set(['b', 'h', 'o', 'n', 'p', 't', 'y'])

集合删除操作方法:remove
set(['y', 'python', 'b', 'o'])
>>> a.remove('python')
>>> a
set(['y', 'b', 'o'])

python set() 集合操作符号、数学符号

集合的交集、合集(并集)、差集,了解python集合set与列表的这些非常好用的功能前,要先了解一些集合操作符号

 

# Author zxm
list1='1352406'
list1 =set(list1)
list2=set('2046')

print(list1,list2)
#交集
print('交集'.join('=='))
print(list1.intersection(list2))
print(list1&list2)
#并集
print('并集'.join('=='))
print(list1.union(list2))
print(list1|list2)
# 差集
print('差集'.join('=='))
print(list1.difference(list2))
print(list2.difference(list1))
print(list1-list2)
print('反向差集'.join('=='))
print(list1.symmetric_difference(list2))
print(list2.symmetric_difference(list1))
print(list1^list2)
print('子集'.join('=='))
print(list1.issubset(list2))
print(list2.issubset(list1))

  输出对应结果

{'6', '2', '1', '0', '4', '3', '5'} {'6', '0', '2', '4'}
=交集=
{'6', '0', '2', '4'}
{'6', '0', '2', '4'}
=并集=
{'6', '2', '1', '0', '4', '3', '5'}
{'6', '2', '1', '0', '4', '3', '5'}
=差集=
{'1', '3', '5'}
set()
{'1', '3', '5'}
=反向差集=
{'1', '3', '5'}
{'1', '3', '5'}
{'1', '3', '5'}
=子集=
False
True

  操作

print('基本操作'.join('=='))
print('添加'.join('=='))
list2.add('8')
print(list2)
list2.update([6,7,8])
print(list2)
list2.remove('8')
print(list2)
print(len(list2))
if '4' in list2:
    print('找到成员')
if '1' not in list2:
    print('不是成员')
print(list2.pop())
print(list2.pop())
print(list2.pop())
print(list2)
list2.discard('8')
print(list2)
list2.discard('888')
print(list2)
list2.remove('8')
print(list2)
list2.remove('888')
print(list2)

  输出结果:

=基本操作=
=添加=
{'8', '0', '2', '6', '4'}
{'8', '0', 6, 7, 8, '2', '6', '4'}
{'0', 6, 7, 8, '2', '6', '4'}
7
找到成员
不是成员
0
6
7
{8, '2', '6', '4'}

  文件

open   r,w,a r+,w+,a+,rb,wb,ab
#读文件,并打印内容
f = open("one_filename",encoding="utf-8")
data =f.read()
print(data)
f.close()
# a = append 追加内容
f = open("yesterday2",'a',encoding="utf-8") #文件句柄
f.write("\nwhen i was young i listen to the radio\n")
f.close()
#文件的一些方法
print(f.tell())
print(f.encoding)
for i in range(3):
    print(f.readline())
#f = open("yesterday",'r+',encoding="utf-8") #文件句柄 读写
#f = open("yesterday2",'w+',encoding="utf-8") #文件句柄 写读
#f = open("yesterday2",'a+',encoding="utf-8") #文件句柄 追加读写
#f = open("yesterday2",'wb') #文件句柄  二进制文件
#f.write("hello binary\n".encode())
#f.close()

 操作文件

import sys,time
#
# for i in range(20):
#     sys.stdout.write('#')
#     sys.stdout.flush()
#     # time.sleep(0.1)
#
# f =open('yesterday','w',encoding='utf-8')
# f.seek(10) #光标移动10
# f.truncate() #清空
# f.truncate(10)#从第10位清空
# f.close()
f =open('yesterday2','r',encoding='utf-8')
f_new =open('yesterday2.bak','w',encoding='utf-8')
for line in f:
    if '我尝到了舌尖泪水的苦涩滋味' in line :
        line=line.replace("苦涩","香甜")
        f_new.write(line)
    else:
        f_new.write(line)

f_new.close()
f.close()
yesterday2的文件内容
我尝到了舌尖泪水的苦涩滋味
The time has come for me to pay for yesterday
终于到了付出代价的时间 为了昨日
When I was young
当我年少轻狂
yesterday2.bak的文件修改后的内容
我尝到了舌尖泪水的香甜滋味
The time has come for me to pay for yesterday
终于到了付出代价的时间 为了昨日
When I was young
当我年少轻狂

 

with语句

为了避免打开文件后忘记关闭,可以通过管理上下文,即:

with open('log','r') as f:
     
    ...
如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。

with open('log1') as obj1, open('log2') as obj2:
    pass

 

字符编码与转码

 

本节内容
1、函数基本语法及特性
2、参数与局部变量
3、返回值
4、递归
5、匿名函数
6、函数式变成介绍
7、高阶函数

1、面向对象: 华山派---》类---》class

2、面向过程:少林派--》过程--》def

3、函数式编程:逍遥派--》函数--》def

函数的文章

https://www.cnblogs.com/vamei/archive/2012/06/01/2529500.html

#函数
def func1():
    """testing1"""
    print('in the func1')
    return 0
#过程
def func2():
    '''testing2'''
    print('in the func2')

x=func1()
y=func2()

print('from func1 return is %s' %x)
print('from func2 return is %s' %y)
import  time
def logger():
    time_format = '%Y-%m-%d %X'
    time_current = time.strftime(time_format)
    with open('a.txt','a+') as f:
        f.write('%s end action\n' %time_current)

def test1():
    print('in the test1')

    logger()
def test2():
    print('in the test2')

    logger()
def test3():
    print('in the test3')
    logger()

test1()
test2()
test3()
def test1():
    pass

def test2():
    return 0

def test3():
    return 0,'hello',['a','b','c'],{'name':'zxm'}

x=test1()
y=test2()
z=test3()