python_d03

01 集合类型

# pythoners=['艾利克斯','wxx', 'egon','吴三江','oldboy']
# linuxer=['wxx','吴三江','张大炮','王全蛋']

# pl=[]
#
# for name in pythoners:
#     if name in linuxer:
#         pl.append(name)
# print(pl)

# 1、用途
# 1.1、关系运算
# 1.2、去重

# 2、定义方式
# {}内用逗号分隔开多个元素,注意注意注意:
# 2.1每一个元素必须为不可变类型
# 2.2集合内的元素不能重复
# 2.3集合无序
# s={} #定义空字典
# s=set()#定义空集合
# print(s,type(s))

# s={1, 'a', 3}   #{'a', 1, 3} <class 'set'>
# s={1,'a',3.1,[1,2]}
# print(s,type(s))
# s={1, 'a', 3,[1,2]}   #报错,list是可变的
# print(s,type(s))

# s={1,2,2,2,2,22,222}
# print(s)  #{1, 2, 222, 22}

# s={'a', 'asdfsa', (1,2), 1, 3.1}
# print(s)  #{'a', (1, 2), 1, 3.1, 'asdfsa'}

# for item in s:
#     print(item)

# 补充:
# 可变类型=》不可hash
# 不可变类型=》可hash

# 优先掌握的操作:
# pythoners={'艾利克斯','wxx', 'egon','吴三江','oldboy'}
# 1、长度len
# print(len(pythoners))

# 2、成员运算in 和 not in
# print('wxx' in pythoners)

# 关系运算
pythoners={'艾利克斯','wxx', 'egon','吴三江','oldboy'}
linuxer={'wxx','吴三江','张大炮','王全蛋'}

# 求两个集合的共同部分:交集&
# print(pythoners & linuxer)
# print(pythoners.intersection(linuxer))

# 求并集
# print(pythoners | linuxer)
# print(pythoners.union(linuxer))

# 差集
# print(pythoners - linuxer)
# print(pythoners.difference(linuxer))
# print(linuxer - pythoners)

# 对称差集(没有同时报两门课的人)
# print(pythoners ^ linuxer)
# print(linuxer ^ pythoners)

# print(pythoners.symmetric_difference(linuxer))

# s1={1,2,3}
# s2={1,2}

# print(s1>=s2)
# print(s2<=s1)

# 需要掌握的操作
# s1={'a','b','c'}
# s2={'b','d','f'}
# s1.difference_update(s2)
# print(s1)

# s1={'a','b','c'}
# print(id(s1))
# s1.add('d')
# print(s1,id(s1))   #id未改变

# res=s1.remove('b')
# print(res)   #remove 单纯移除,没有返回值
# print(s1)
# s1.remove('ddddd')    #元素不存在会报错

# res=s1.discard('b')
# print(s1)
# print(res) #discard单纯移除,没有返回值
# s1.discard('b')  #元素不存在不会报错

# res=s1.pop()
# print(res)   #随机删除,有返回值

# 需要了解的
# s1={1,2,3}
# s1.update({3,4,5})
# print(s1)

# s1.clear()
# print(s1,type(s1))

# s1={1,2,3}
# s2={4,5,6}
# print(s1.isdisjoint(s2))   #True,两个没有重复部分为True

# 用集合去重,局限性很强
# 1、不能保证原数据类型的顺序
# 2、元数据类型中包含的元素必须全部为不可变类型
# names=['alex', 'egon', 'wxx', 'wxx', 'wxx']
# print(list(set(names)))     #set(names)去重, list(set(names))转化为list

l=[
    {'name':'egon','age':18, 'gender':'male'},
    {'name':'alex','age':28,'gender':'male'},
    {'name':'egon','age':28,'gender':'male'},
    {'name':'egon','age':18, 'gender':'male'},
    {'name':'egon','age':18, 'gender':'male'},
    {'name':'egon','age':18, 'gender':'male'},
]
# print(type(l),l)
# s=set()
# new_l=[]
# for d in l:
#     values=(d['name'],d['age'],d['gender'])
#     print(values)
#     if values not in s:
#         s.add(values)
#         new_l.append(d)
# print(new_l)

s=[]
for d in l:
    if d not in s:
        s.append(d)
print(s)

03 文件处理

# 文件对象=open(文件的路径,mode=文件的打开模式,encoding=字符编码)

# 强调:下述操作涉及两方面的资源
# 1、操作系统需要打开文件
# 2、f就是一个Python的变量

# f=open(r'/home/m/oldboy/d02/08 字典类型.py', mode='r',encoding='utf-8')
# data=f.read()
# print(data)
# print(f)
# f.close()
#
# print(f)
# f.read()  #f文件已经关闭,报错。

# with open('/home/m/oldboy/d02/08 字典类型.py', mode='r',encoding='utf-8') as f:
#     data=f.read()
#     print(data)

with open('/home/m/oldboy/d02/08 字典类型.py',mode='r',encoding='utf-8') as f,\
    open('/home/m/oldboy/d02/06 列表类型.py',mode='r',encoding='utf-8') as f1:
    data=f.read()
    print(data)

04 文件的打开模式

# r:默认的打开模式,只读模式,文件如果不存在则报错
# with open('test.py',mode='r',encoding='utf-8') as f:
#     print(f.readable())
#     print(f.writable())
#     f.write('hello')  #not writable


    # print('第一次')
    # print(f.read())
    # print('第二次')
    # print(f.read())     #read是一次读完了,第二次没有读出内容

    # print(f.readline()) #一次读一行
    # print(f.readline())
    # print(f.readline())

    # print(f.readlines())   #读出来变成列表,有换行符

# w:只写模式,文件存在则清空,文件如果不存在则创建
# with open('test.py',mode='w',encoding='utf-8') as f:
#     print(f.readable())
#     print(f.writable())
#     f.write('m is so cute\n')
#     f.write('m is so nice')

    # info=['m is experienced\n', 'm is lovely\n']
    # # for line in info:
    # #     f.write(line)
    # f.writelines(info)  #writelines相当于一个for循环,把列表或者元组写进去
    # f.write('a\nb\nc\n')

# a:只追加写模式,文件存在指针直接移动到文件末尾,文件如果不存在则创建
#  with open('access.log',mode='a',encoding='utf-8') as f:
# with open('test.py',mode='a',encoding='utf-8') as f:
#     print(f.readable())
#     print(f.writable())
#     f.write('test_aaa\n')
#     f.writelines('test_bbb\n')

# 控制文件读写单位的两种模式:
# 1、t:默认的模式
# with open('c.txt', mode='wt',encoding='utf-8') as f:
#     f.write('放大镜的房间爱来的快放假啦')

# with open('1.png',mode='rt',encoding='utf-8') as f:
#     f.read()

# 2、b:二进制模式,该模式下读写都是bytes,该模式下不能指定encoding参数
# with open('1.png',mode='rb') as f:
#     data=f.read()
#     print(data)
#     print(type(data))

# with open('c.txt',mode='rb') as f:
#     data=f.read()
#     print(data)
#     print(data.decode('utf-8'))

 

05 循环读取文件内容

# with open('test.py','r',encoding='utf-8') as f:
    # for line in f:
    #     print(line,end='')

# with open('test.py','rb') as f:
#     for line in f:
#         print(line,end='')

# with open('1.png', 'rb') as f:
#     for line in f:
#         print(line,end='')

copy.py

import sys,os
# print(sys.argv)
src_file_path=sys.argv[1]
if not os.path.isfile(src_file_path):
    print("file doesn't exist")
    sys.exit()
dst_file_path=sys.argv[2]

with open(r'%s' %src_file_path,'rb') as read_f,\
    open(r'%s' %dst_file_path,'wb') as write_f:
    # write_f.write(read_f.read())  #文件过大会导致机器很慢
    for line in read_f:
        write_f.write(line)

执行复制操作:

06修改文件内容

# with open('c.txt', 'rt+', encoding='utf-8') as f: #rt+,+可读也可写
#     f.seek(6) #移动的单位是字节
#     f.write('[修改内容修改内容]')

# 修改文件内容方式一:
# with open('c.txt', 'r',encoding='utf-8') as f:
#     data=f.read()
#     # print(type(data),data)
#     data=data.replace('劳斯莱斯','mmm')
#     print(data)
#
# with open('c.txt','w',encoding='utf-8') as f:
#     f.write(data)

# 修改文件内容方式二:
import os

with open('c.txt', 'r', encoding='utf-8') as f,\
    open('c.txt.swap','w',encoding='utf-8') as f_new:
    for line in f:
        if 'mmm' in line:
            line=line.replace('mmm','aaa')
        f_new.write(line)

os.remove('c.txt')
os.rename('c.txt.swap','c.txt')

07函数

name_from_db='ddd'
pwd_from_db='111'

def auth():
    name=input('用户名>>:').strip()
    pwd=input('密码>>:').strip()
    role=input('角色>>:').strip()

    if name == name_from_db and pwd == pwd_from_db:
        print('login successfully')
    else:
        print('error')

print(auth)

auth()

08 函数的定义与调用

# 1、语法
# def 函数名(参数1,参数2,...):
#     """
#     函数的文档注释
#     :param 参数1:参数1的作用
#     :param 参数2:参数2的作用
#     :return:返回值的描述
#     """
#     代码1
#     代码2
#     代码3
#     ...:
#     return返回值

# 2、函数的使用必须遵循:先定义,后调用
# 定义函数相当于定义了一个变量,没有事先定义函数,而直接使用,相当于在引用一个不存在的变量名
#
# 2.1在函数定义阶段只检测语法,不执行代码
# def foo():
#     # xxxxx
#     if

# 2.2定义阶段
# def bar():
#     print('from bar')
#
# def foo():
#     print('from foo')
#     bar()
#
# # 调用阶段
# foo()
# bar()

# 定义阶段
# def foo():
#     print('from foo')
#     bar()
# # foo() #会报错,bar未定义
# def bar():
#     print('from bar')

# 调用阶段
# foo()

# def print_star():
#     print('*'*1000)
#
# print_star()


# def max(x,y):
#     if x>y:
#         print(x)
#     else:
#         print(y)
#
# max(3,11)

def max(x,y):
    if x>y:
        return(x)
    else:
        return (y)
res=max(3,11)
print(res)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值