day3

集合

s = set([3,5,9,10])      #创建一个数值集合  
  
t = set("Hello")         #创建一个唯一字符的集合  


a = t | s          # t 和 s的并集  
  
b = t & s          # t 和 s的交集  
  
c = t – s          # 求差集(项在t中,但不在s中)  
  
d = t ^ s          # 对称差集(项在t或s中,但不会同时出现在二者中)  
  
   
  
基本操作:  
  
t.add('x')            # 添加一项  
  
s.update([10,37,42])  # 在s中添加多项  
  
   
  
使用remove()可以删除一项:  
  
t.remove('H')  
  
  
len(s)  
set 的长度  
  
x in s  
测试 x 是否是 s 的成员  
  
x not in s  
测试 x 是否不是 s 的成员  
  
s.issubset(t)  
s <= t  
测试是否 s 中的每一个元素都在 t 中  
  
s.issuperset(t)  
s >= t  
测试是否 t 中的每一个元素都在 s 中  
  
s.union(t)  
s | t  
返回一个新的 set 包含 s 和 t 中的每一个元素  
  
s.intersection(t)  
s & t  
返回一个新的 set 包含 s 和 t 中的公共元素  
  
s.difference(t)  
s - t  
返回一个新的 set 包含 s 中有但是 t 中没有的元素  
  
s.symmetric_difference(t)  
s ^ t  
返回一个新的 set 包含 s 和 t 中不重复的元素  
  
s.copy()  
返回 set “s”的一个浅复制  

 集合代码

__author__ = 'weihui'
#集合无序
list_1 = [1,2,3,1,4,2,5,2]
list_1 = set(list_1)
list_2 = set([2,5,4,5,3,9])
print(list_1,list_2)
print(list_1.intersection(list_2))#交集
print(list_1 & list_2)

print(list_1.union(list_2))#并集
print(list_1 | list_2)

print(list_1.difference(list_2))#差集
print(list_1 - list_2)

print(list_1.issuperset(list_2))#父集
print(list_1.issubset(list_2))#子集

print(list_1.symmetric_difference(list_2))#对称差
print(list_1 ^ list_2)

print(list_1.isdisjoint(list_2))#无交集返回true

list_1.update([7,32,12])
list_1.add(22)
print(list_1)

list_1.remove(2)
print(len(list_1))
print(list_1)

for i in list_1:
    print(i)

 

文件操作

打开文件的模式有:

    r,只读模式(默认)。
    w,只写模式。【不可读;不存在则创建;存在则删除内容;】
    a,追加模式。【可读;   不存在则创建;存在则只追加内容;】

"+" 表示可以同时读写某个文件

    r+,可读写文件。【可读;可写;可追加】
    w+,写读
    a+,同a

"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)

    rU
    r+U

"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

    rb
    wb
    ab

 

文件代码

文件修改
__author__ = 'weihui'

f = open('java2','r',encoding='utf-8')
f_new = open('java3','w',encoding='utf-8')
for line in f:
    if "234" in line:
       line = line.replace("234","换的")
    f_new.write(line)
f.close()
f_new.close()
       

文件内容
__author__ = 'weihui'
print("1,add\t","2,del\t","3,read\t")
choose  = int(input("选择:"))
while choose <= 3 & choose >0:
    if choose == 1:
        with open('java3','a+',encoding='utf-8') as f1_new:
            f1_new1 = input("增加的内容是:")
            f1_new.write(f1_new1)
    elif choose == 2:
        with open('java3','r+',encoding='utf-8') as f2_new:
            f2_new2 = input("删除的内容是:")
            for line in f2_new:
                if f2_new2 in line:
                    del line
    else:
        with open('java3','r',encoding='utf-8') as f3_new:
            f3_new.read()
else:
    print("input error")

 文件相关

__author__ = 'weihui'
f = open("java2",'r+',encoding="utf-8")
#a追加 r读 w写 wb 二进制
#data = f.read()
#data2 = f.read()
#f.write()
#for i in range(5):
#   print(f.readline())
'''
for index,line in enumerate(f.readline()):
    if index == 9:
        print('---分割线---')
        continue
    print(line)
'''
'''
count = 0
for line in f:
    if count == 9:
        print('-----分割线-----')
        count += 1
        continue
    print(line.strip())
    count += 1
'''
print(f.tell())
print(f.readline())
print(f.readline())
print(f.readline())
print(f.tell())
f.seek(2)
print(f.readline())
f.close()

 

编码解码

__author__ = 'weihui'
import sys
s = "你哈"
s_gbk = s.encode("gbk")
print(s_gbk)
print(s.encode())
gbk_to_utf8 = s_gbk.decode("gbk").encode("utf-8")
print("utf8",gbk_to_utf8)


__author__ = 'weihui'
# -*-coding:gbk-*-
import sys
s = "你哈"
print(s.encode("gbk"))
print(s.encode("utf-8"))
print(s.encode("utf-8").decode("utf-8").encode("gb2312").decode("gb2312"))

 

函数

一、
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():
    print('in the test1')

def test2():
    print('in the test2')
    return 0
def test3():
    print('in the test3')
    #return 1,'hello',['abc','bcd'],{'name':'abc'}
    return  test2


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

 函数参数

#*args:接受N个位置参数,转换成元组形式
# def test(*args):
#     print(args)
#
# test(1,2,3,4,5,5)
# test(*[1,2,4,5,5])#  args=tuple([1,2,3,4,5])

# def test1(x,*args):
#     print(x)
#     print(args)
#
# test1(1,2,3,4,5,6,7)


#**kwargs:接受N个关键字参数,转换成字典的方式
# def test2(**kwargs):
#     print(kwargs)
#     print(kwargs['name'])
#     print(kwargs['age'])
#     print(kwargs['sex'])
#
# test2(name='alex',age=8,sex='F')
# test2(**{'name':'alex','age':8})
# def test3(name,**kwargs):
#     print(name)
#     print(kwargs)
#
# test3('alex',age=18,sex='m')

# def test4(name,age=18,**kwargs):
#     print(name)
#     print(age)
#     print(kwargs)
#
# test4('alex',age=34,sex='m',hobby='tesla')

def test4(name,age=18,*args,**kwargs):
    print(name)
    print(age)
    print(args)
    print(kwargs)
    logger("TEST4")



def logger(source):
    print("from %s" %  source)

test4('alex',age=34,sex='m',hobby='tesla')

 进度条

import sys,time

for i in range(20):
    sys.stdout.write("#")
    sys.stdout.flush()
    time.sleep(0.1)

 

posted @ 2017-11-12 16:33  whz_it  阅读(156)  评论(0编辑  收藏  举报