码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础2

时间:2017-10-19 21:18:13      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:交集   map   translate   删除   amp   内容   isa   section   命名   

1 列表、元组操作

列表可以对数据实现最方便的存储、修改等操作

 

1 names = [Jack, Leon,Eric]

 

通过下表访问列表中的元素,下标从0开始计数

1 names = [Jack,Leon,Eric]
2 a = names[0]
3 b = names[1]
4 c = names[-1]
5 d = names[-2]
6 print(a, b, c, d)

切片:取多个元素

 1 names = [Alex,Leon,Eric,"Rain","Tom","Amy"]
 2 a = names[1: 4]#取下标1至下标4之间的数字,包括1,不包括4
 3 
 4 b = names[1: -1]#取下标1至-1的值,不包括-1
 5 
 6 c = names[0: 3]
 7 d = names[ :3]#如果是从头开始取,0可以忽略,跟上句一样
 8 
 9 e = names[3: ]#如果想取最后一个,必须不能写-1,
10 
11 f = names[3: -1]#这样-1就不会包含
12 
13 g = names[0::2]#后面的2代表,每隔一个元素,取一个
14 h = names[::2]#和上句效果一样
15 
16 print(a, b, c, d, e, f, g, h)

追加——append方法

1 names.append("我是新来的")
2 print(names)

插入——insert方法

1 names.insert(2,"插入1")

修改——索引赋值

1 names[2] = "换人了"

删除——del函数、remove方法、pop方法

1 del names[2]#删除指定位置元素
2 names.remove("Eric")#删除指定元素
3 names.pop()#删除最后一个元素

扩展——extend方法

1 b = [1, 2, 3]
2 names.extend(b)
3 print(names)

拷贝——copy方法

1 name_copy = names.copy()
2 print(name_copy)

统计——count方法

1 count1 = names.count("Leon")
2 print(count1)

排序、反转——sort方法、reverse方法

1 names.sort()
2 print(names)
1 names.reverse()
2 print(names)

获取下标——index方法

1 index1 = names.index("Leon")
2 print(index1)

元组(只读列表)

1 names2 = ("aaa", "bbb", "ccc")

元组只有count、index两个方法

2 字符串操作

特性:不可修改

首字母大写

 

1 name = "leon nie"
2 print(name.capitalize())

 

大写全部变小写

1 print(name.casefold())

 

center方法

 

1 print(name.center(50,"-"))
#结果
2 ---------------------leon nie---------------------

 

统计次数

1 print(name.count("e"))

将字符串编码成byte3格式

1 print(name.encode())
2 #输出
3 bleon nie

判断是否以XX结尾

1 print(name.endswith("n"))

format方法

1 msg = "my name is {}, and age is {}"
2 print(msg.format("leon", 22))
3 
4 msg = "my name is {0}, and age is {1}"
5 print(msg.format("leon", 22))
6 
7 msg = "my name is {name}, and age is {age}"
8 print(msg.format(age = 22, name = "lmsg = "my name is {name}, and age is {age}"
print(msg.format_map({"name":"alex","age":22

 

 1 #按顺序写入
 2 msg = "my name is {}, and age is {}"
 3 print(msg.format("leon", 22))
 4 
 5 #指定顺序写入
 6 msg = "my name is {0}, and age is {1}"
 7 print(msg.format("leon", 22))
 8 
 9 #指定元素写入
10 msg = "my name is {name}, and age is {age}"
11 print(msg.format(age = 22, name = "leon"))
12 
13 #字典写入
14 msg = "my name is {name}, and age is {age}"
15 print(msg.format_map({"name":"alex","age":22}))

返回元素所在的索引

1 print(msg.index("age"))

是否为纯数字和字母

1 print(9aA.isalnum())

是否整数

1 print(9.isdigit())

join方法

1 print("|".join(["aaa","bbb","ccc"]))
#连接列表

两个字符串对应转换,可以用于密码?——maketrans方法

1 intab = "aeiou"
2 outtab = "12345"
3 str = "this is string example...wow!!!"
4 
5 trantab = str.maketrans(intab, outtab)
6 
7 print(str.translate(trantab))

替换函数——replace

1 print("alex li, chinese name is lijie".replace("li","LI",1))

大小写互换——swaplace

1 print("aleX li".swapcase())

用0填充指定长度——zfill

1 msg = "leon"
2 print(msg.zfill((40)))

左填充、右填充——ljust、rjust

1 print(msg.ljust(40,"-"))
2 print(msg.rjust(40,"+"))

检查是否可以作为标识符,即是否符合变量命名规则

1 print(msg.isidentifier())

3 字典操作

key-value数据类型

语法:

1 info = {
2     stu1101:aaa,
3     stu1102:bbb,
4     stu1103:ccc,
5 }

字典的特性:

  • dict是无序的
  • key必须是唯一的

增加

1 info["stu1104"] = "ddd"
2 print(info)

修改

1 info["stu1101"] = "XXX"

删除

1 info.pop("stu1101")
2 del info[stu1103]
3 info.popitem()#随机删除

查找

1 #判断是否在字典中
2 print("stu1102" in info)
3 
4 #获取,如果没有该key返回None
5 print(info.get("stu1102"))
6 
7 #获取,如果key不存在,报错
8 info["stu1105"]

setdefault和update

a = info.values()
b = info.keys()
c = info.setdefault("stu1106","Alex")
#setdefault方法,key-value存在,更新value。不存在,添加。
x
= {1:2, 3:4, "stu1102":"HAHA"} info.update(x)#update方法对有key的value进行更新,没有的添加

items

1 print(info.items())
2 #返回字典中的所有key-value对

循环dict

1 for key in info:
2     print(key,info[key])

4 集合操作

集合是一个无序的,不重复的数据组合,主要作用如下:

  • 去重,把一个列表变成集合,就自动去重了
  • 关系测试,测试两组数据之前的交集、差集、并集等关系
 1 s = set([3, 5, 9, 10])
 2 t = set("Hello")
 3 
 4 a = t | s #并集
 5 b = t & s #交集
 6 b = s.intersection(t)#st的交集
 7 
 8 c = t - s #差集,在t中,不在s中
 9 c = t.difference(s)#
10 
11 d = t ^ s #对称差集(项在t或s中,但不会同时出现在二者中)
12 
13 t.add(x)  #在t中添加一项
14 s.update([10, 37, 42])#在s中添加多项
15 
16 t.remove(H)#删除一项
17 
18 len_s = len(s)#set的长度
19 
20 print(x in s)#判断x是否是s的成员
21 print(x not in s)#判断x是否不是s的成员
22 print(s.issubset(t))#判断s是否是t的子集
23 print(t.issuperset(s))#判断t是否是s的父集
24 print(s.union(t))#返回包含s、t每一个元素的set,并集

5 文件操作

文件操作流程

  • 1 打开文件,得到文件句柄并赋值给一个变量
  • 2 通过句柄对文件进行操作
  • 3 关闭文件.

基本操作

f = open(lyrichaha,encoding=UTF-8)#要指定解码方式UTF-8,不然无法打开
first_line = f.readline()#读一行
print(first line: , first_line)
print(我是分割线.center(50,-))
data = f.read()#读取剩下的所有内容,文件大时不要用
print(data)
f.close()

 

Python基础2

标签:交集   map   translate   删除   amp   内容   isa   section   命名   

原文地址:http://www.cnblogs.com/hellojack/p/7694628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!