python第三周 字符串,字典,列表

一、字符串的


name = "My \tname is {name}  and {age}"

print(name.capitalize())    #首字母大小 capita首都的意思
print(name.count("r"))      #count 查找字符串中某个字符的数量
print(name.casefold())      #没什么用处
print(name.center(50,"-"))  #在字符未达到所要求的数量后,用后面的字符补全  内容在中间。
print(name.endswith("on"))  #判断结尾是否是该字符,可过滤如判断该邮件是不是以.com或.cn为结尾。
print(name.expandtabs(tabsize=10))  #  字符串中需要加 \t tabsize=10 代表有10个空格。
print(name[name.find("is"):])   #在name值字符串中找到is字眼,并对该字符前的进行切片。:号后输入下字符下位坐标可以截止当前,否则全取
print(name.format(name="Byron",age=23))
print(name.format_map( {"name":"byron","age":20} )) #此处在字典里会细讲一般不用此种表达式。
print("a12".isalnum())  #判断是否是阿拉伯数字或字母,可以是汉字,如果是 ture 否则False
print("awerert忘情号".isalpha())   #判断为是否是全英文,包含文字符,数字则False
print("13".isdecimal())    #decimal英文意思是小数,但是实际非小数,只能是数字整数和isdigit作作类似。
print("我们a".isidentifier())   #是否包含数字字符,否则为False,可以含中文和英文字符
print("A".islower())  #判断字符是否是小字,否则显示False
print("123".isnumeric())  #判断是否为数字和isdigit像似但一般用isdigit即可。
print(" ".isspace())    #判断是不是空格。
print("My Name Is ".istitle()) #判断第一个字母是否是大小。
print("my name is ".isprintable())  #tty file,drivefile
print("MY".isupper()) #判断字母是不是大写,如果是大写就是True
print("+".join(["1","2","3"]))
print(name.ljust(50,"!"))   #右边字符补偿。
print(name.rjust(50,"$"))   #左边字符补偿。
print("BYron".lower())      #大小改小写。
print("bryon".upper())      #小写改大写。
print("\nByron".lstrip())   #取消左边换行。
print("Byron\n".rstrip())   #取消右边换行。
print("  Byron  ".strip())    #取消左边和右边的换行和空格。
s = str.maketrans("abcdefghlk",'1234567890')
print("Byron   he".translate(s))

print("Byron He".replace("e","H"))  #替换的意思。
print("Byron He".rfind("H"))    #从左往右查找,查找到最后一位取下标。
print("1+2+3+4".split("+"))    #把数字提出来,字符。print("1+2\n+3+4".splitlines())  #linux \n  windows \win\n 换行的意思!

 

print("Byron HE".swapcase())print("Byron He".title())print("Byron He".zfill(50)) #十六进制的时候可以补全。
二、字典的操作:

字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。


语法:


info = {
    'stu1101': "TengLan Wu",
    'stu1102': "LongZe Luola",
    'stu1103': "XiaoZe Maliya",
}

字典的特性:


  • dict是无序的
  • key必须是唯一的,so 天生去重
增加:info["stu1104"] = "苍井空"

修改:info['stu1101'] = "武藤兰"  #如果存在测修改,如果不存在则增加。

删除:info.pop("stu1101") #标准删除姿势    del info['stu1103'] #换个姿势删除

查找:"stu1102" in info #标准用法   

 

info["stu1102"] #如果字典里不存在会报错!

 

 

info.get("stu1102")  #获取  通过此方法如果字典内不存在,则返回None,不会报错

 

多级字典嵌套及操作:

#values
>>> info.values()
dict_values(['LongZe Luola', 'XiaoZe Maliya'])

#keys
>>> info.keys()
dict_keys(['stu1102', 'stu1103'])


#setdefault
>>> info.setdefault("stu1106","Alex")
'Alex'
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
>>> info.setdefault("stu1102","龙泽萝拉")
'LongZe Luola'
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}


#update
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
>>> b = {1:2,3:4, "stu1102":"龙泽萝拉"}
>>> info.update(b)
>>> info
{'stu1102': '龙泽萝拉', 1: 2, 3: 4, 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}

#items
info.items()
dict_items([('stu1102', '龙泽萝拉'), (1, 2), (3, 4), ('stu1103', 'XiaoZe Maliya'), ('stu1106', 'Alex')])


#通过一个列表生成默认dict,有个没办法解释的坑,少用吧这个
>>> dict.fromkeys([1,2,3],'testd')
{1: 'testd', 2: 'testd', 3: 'testd'}

循环dict:

#方法1
for key in info:
    print(key,info[key])

#方法2
for k,v in info.items(): #会先把dict转成list,数据里大时莫用
    print(k,v)

 

三、集合操作

 集合是无序的!

  list_1 = [1,2,5,6,7,8,9]

  list_1 = set(list1)

 

  list_2 = set[0,3,4,6,8,10]

交集:#intersection (交叉) &

  (list_1.intersection(list_2))

并集:union  (合并)  | 管道符

  (list_1.union(list_2))

差集: difference    - 减号

  (list_1.difference(list_2))   求1里面有的2里面没有的!

子集:issubset

父集:issuperset

 

对称差集:symmetric_difference (对称)取互相没有的给取出。 ^ 上尖号

交集判断:isdisjoint

集合的增 加 删除

.add  增加单项

.update  增加多项

.remove(" ") 删除

.pop 随机删除

.discard 如果集合中没有不会报错,会返回none 其它方式会有报错删除。

x.in s   判断x在不在s 里,字符列表,字典都可以用此方法查

 

list_1 = set([0,1,3,66,100])
list_2 = set([0,2,4,88,100])
list_3 = set([0,1,100])

print(list_1,list_2)
print(len(list_1))   #set的长度
print(0 in list_1)      #测试0是否是list_1的成员  是 False 不是 True。
print(1 not in list_1)  #测试2是否不是list_1的成员   不是True  是则False

print(list_3.issubset(list_1))  #测试list_3中的元素都在list_1中,如果都在则是True,如果不在则是False
print(list_1.issuperset(list_3))  #测试list_1中的元素都在list_3中,如果都在则是True,如果不在则是False





print("----交集----") #去掉未重复的,只显示重复的值。
jj = list_1 & list_2
print(list_1.intersection(list_2))
print(jj)

print("---并集---")   #如果前面一级输出,如果第二个表里有第一个里面的则不显示。
bj = list_1 | list_2
print(list_1.union(list_2))
print(bj)
print("---差集---")   #返回新的set list1有的,list2里没有的
print(list_1.difference(list_2))

print("---子集-父集--")
list_3 = set([0,2,4])
print(list_3.issubset(list_2))
print(list_2.issuperset(list_3))

print("---对称差集---") #去掉重复的,只返回相互没有的!
print(list_1.symmetric_difference(list_2))

print("---增加---")
list_1.add("166")   #向集合中添加一个值
print(list_1)

print("---增加多项---")

list_1.update([110,120,130])
print(list_1)

print("---删除---")
print(list_1.remove(0))
print(list_1)

print(list_1.pop())   #随机删除

 

 

 四、文件的操作

 

#data = open("file",encoding="utf-8").read()
f = open("file2","r",encoding="utf-8") #文件句柄中包含了文件名,字符编码,文件大小,磁盘起始位。
f = open("file2","w",encoding="utf-8") #写的意思!但是注意了,此处写的话会把之前给清除了,可以用此方法新建。
f = open("file2","a",encoding="utf-8")#a = append 追加的意思。

 f = open("file","r",encoding="utf8") #文件句柄中包含了文件名,字符编码,文件大小,磁盘起始位。

 

 

 

 

count = 0
for line in f:
if count == 9:
print("-----我是分割线-----")
count +=1


continue
print(line.strip())  #strip 去空格 去换行
count +=1

 

 

 

 

 

文件的增删改查

 f = open("file","rb") #什么情况下用rb? 当网络传输的时候会用到,网络传输只能用二进制,soft

f.write("----------diao---------\n")
f.write("----------diao---------1\n")
f.write("----------diao---------2\n")
f.write("----------diao---------3\n")
print(f.tell())
f.seek(10)
print(f.readline())
print(f.readline())
f.write("秀梅 秀美 凑满膛")
f.closed

f.seek(5)  从哪个位置开始
f.truncate(20)  #切割

print(f.tell())  #光标默认启示位
print(f.readline())
print(f.tell())     #读过之后光标所在位置,并给出所在多少个字符位。
f.seek(5)           #从第光标位置开始读。

print(f.readlines())
print(f.tell())     #.tell 一般和 .seek配合使用,并且只有二进制,字符可以移,
f.flush()       #刷新内存,把存在内存中的的值写入到硬盘上。
print(f.readable())     #判断可不可以读。
print(f.closed) #判断有没有关闭。

print(f.seekable()) #tty,终端设备文件是无法移,相关文件不能移。判断可以不可以移,可以TRUE 否则Flse
f.writable() #判断可写。
print(f.encoding)   #打印打开文件的编码格式。

f = open("file","a",encoding="utf-8")
f = open("file","r+",encoding="utf-8") #r+ 读写模式,可读可写
f = open("file","a+",encoding="utf-8") #a+ 追加写读。
f = open("file","w+",encoding="utf-8")  #w+ 写读模式,一般不用此方法!

f = open("file","rb")       #二进制模式去读,此二进制并不是所说的010101,而是以二进制编码。


# 什么情况下用rb? 当网络传输的时候会用到,网络传输只能用二进制,soft
f = open("file","wb")   #文件句柄b' 字节类型。
f.write("Byron\n".encode()) #转换时不指定编码类形,默认就用程序编码程序。
f.close()

#flush 进度条小程序!
import sys,time
for i in range(10)"
  sys.stdout.write("#")  #stdout 标准输出 stdin标准输入
  sys.stdout.flush     #flush刷新
  time.sleep(0.1)      #输入的时间。

 

 

 

posted @ 2017-05-11 23:38  Byron-He  阅读(359)  评论(0)    收藏  举报