Python-基础语法(二):
1、list方法:
list赋值:test=[1,1,1] test[0]=2 print test
#结果为[2,1,1]
批量赋值:test=[1,2,3] test[2:]=[7,8,9] print test
#结果为[1,2,7,8,9]
删除元素:test=[1,2,3] del test[0] print test
#结果为[2,3]
1.1 append #在列表末尾追加新元素
test=[1,2,3]
test.append(4)
print test #[1,2,3,4]
1.2 count #统计某个元素在列表中出现的次数
test=[‘b’,‘e’,‘be’,‘maybe’,‘be’]
a=test.count(‘e’) #返回1
a=test.count(‘be’) #返回2
1.3 extend #在列表末尾一次追加多个元素
a=[1,2,3]
b=[4,5,6]
c=a.extend(b)
print c #[1,2,3,4,5,6]
1.4 index #定位元素位置,即已知元素,得到元素在列表中的索引号
test=[‘Li’,‘Bob’,‘Jack’,‘Tony’,‘Bill’]
a=test.index(‘Bill’)
print a # 返回 4
1.5 insert #在列表中插入元素
test=[1,2,3,4,5]
test.insert(3,‘insert’)
print test # [1, 2, 3, ‘insert’, 4, 5]
1.6 pop #移除列表的一个元素。默认移除最后一个
test=[1,2,3,4,5]
test.pop()
print test # 返回 [1, 2, 3, 4]
test.pop(0)
print test # 返回 [2, 3, 4]
1.7 remove #移除列表中的第一个匹配项
test=[‘Li’,‘Bob’,‘Jack’,‘Tony’,‘Bill’,‘Li’]
test.remove(‘Li’)
print test # 返回 [‘Bob’, ‘Jack’, ‘Tony’, ‘Bill’, ‘Li’]
1.8 reverse #将列表反向存放
test=[1,2,3,4,5]
test.reverse()
print test # 返回 [5, 4, 3, 2, 1]
1.9 sort #将列表排序,注:使用sort后列表顺序不能按照原始的序列打印,一般做法是定义一个新列表,在sort
a=[4,8,1,28,14,9,5,19]
b=a[:]
b.sort
print a # [4, 8, 1, 28, 14, 9, 5, 19] 保证原序列不改变
print b # [1, 4, 5, 8, 9, 14, 19, 28] 排序后的序列
1.10 高级排序 #sort为python默认排序方法,但是有时需要按照不同的方式排序,sort提供 cmp key 和reverse作为参数
a=[4,8,1,28,14,9,5,19]
b=a[:]
b.sort(reverse=True)
print a # [4, 8, 1, 28, 14, 9, 5, 19] 保证原序列不改变
print b # [28, 19, 14, 9, 8, 5, 4, 1] 排序后的序列
test=[‘Li’,‘Bob’,‘Jack’,‘Tony’,‘Bill’]
test.sort(key=len) #按照元素长度排序
print test #[‘Li’, ‘Bob’, ‘Jack’, ‘Tony’, ‘Bill’]
2、字符串
格式化转换类型:了解python字符串的格式化转换的类型:%d %i %o %u % %x %X %e %E %r %s 等
a=‘i have %d books’ % 45.00
print a # i have 45 books
2.1 find test=‘i am studying python,and i like python.and …’
ss=test.find(‘python’)
print ss #结果为 14 #返回查找内容在字符串最左端的索引,没找到则返回-1
ee=test.find(‘python’,15) #注:find可添加起始和终止位
print ee #返回结果为32
tt=test.find(‘python’,15,20) #添加终止位
print tt #索引15 与 20 之间找不到python 返回 -1
2.2 split #将字符串分割成序列
a=‘1+2+3+4+5’.split(’+’)
print a #[‘1’,‘2’,‘3’,‘4’,‘5’]
2.3 join #join为split的逆方法
dirs=‘usr’,‘bin’,‘python’
a=’/’.join(dirs) #或 ss=’/’ a=ss.join(dirs)
ptint a #结果为’usr/bin/python’
2.4 lower #将字符串转换为小写
2.5 replace #替换字符串
2.6 strip #是常用的处理字符串方法,剔除字符串两侧的空格
a=’ whitespace at the left and right ’
pring a.strip() #‘whitespace at the left and right’
2.7 translate #和replace一样,替换单个字符;
from string import maketrans
table=maketrans(r’pa’,r’PA’)
test =‘i am studying python’
a=test.translate(table)
print a #返回结果 i Am studying Python
3、dict 字典
a=[(‘name’,‘li’),(‘age’,25)]
print a # [(‘name’, ‘li’), (‘age’, 25)]
b=dict(a)
print b # {‘age’: 25, ‘name’: ‘li’}
3.1 clear
test={‘name’:‘Li’,‘age’:28}
test.clear()
print test # {}
3.2 copy #copy默认为浅复制,copy之后,如果’原地’修改字典,原字典改变
a={‘name’:‘Li’,‘uid’:[1,2,3]}
b=a.copy()
b[‘name’]=‘Hyper’
b[‘uid’].remove(2) #修改b,但是a同步受影响
print a # {‘name’: ‘Li’, ‘uid’: [1, 3]}
print b # {‘name’: ‘Hyper’, ‘uid’: [1, 3]}
注. 深度复制
from copy import deepcopy
b= deepcopy(a) #深度复制,修改b,a不受影响
b[‘uid’].remove(2)
print a # {‘name’: ‘Li’, ‘uid’: [1, 2, 3]}
print b # {‘name’: ‘Hyper’, ‘uid’: [1, 3]}
3.3 fromkeys #将列表转换为字典的key [] () 都可以进行转换
a=[‘name’,‘uid’]
b=dict.fromkeys(a)
print a # [‘name’, ‘uid’]
print b # {‘name’: None, ‘uid’: None}
b=dict.fromkeys(a,‘default’) #提供默认值
print b # {‘uname’: ‘default’, ‘uid’: ‘default’}
3.4 get #常用的访问字典的方法
a={‘uname’:‘Li’,‘uid’:‘4’}
print a[‘name’] #字典中没有’name’的key,因此此访问方式会报错
print a.get(‘name’) #返回 None
3.5 update #利用一个字典更新另外一个字典
test={‘name’:‘Bill’,
‘usid’:‘9541’,
‘mark’:‘B’}
uptest={‘usid’:‘5541’}
test.update(uptest)
print test # {‘mark’: ‘B’, ‘name’: ‘Bill’, ‘usid’: ‘5541’}
print uptest # {‘usid’: ‘5541’}uptest.update(test)
print test # {‘mark’: ‘B’, ‘name’: ‘Bill’, ‘usid’: ‘5541’}
print uptest # {‘mark’: ‘B’, ‘name’: ‘Bill’, ‘usid’: ‘5541’}
3.6 items 和 iteritems #以列表方式返回
test={‘name’:‘Bill’,
‘usid’:‘9541’,
‘mark’:‘B’}
a=test.items()
print a #[(‘mark’, ‘B’), (‘name’, ‘Bill’), (‘usid’, ‘9541’)]
b=test.iteritems()
print list(b) #[(‘mark’, ‘B’), (‘name’, ‘Bill’), (‘usid’, ‘9541’)]
3.7 keys 和 iterkeys #将字典中的键以列表的形式返回
3.8 values 和 itervalues #将字典中的值以列表的形式返回
3.9 pop #通过键移除字典中的元素
test={‘name’:‘Bill’,
‘usid’:‘9541’,
‘mark’:‘B’}
test.pop(‘name’)
print test # {‘mark’: ‘B’, ‘usid’: ‘9541’}
3.10 popitem #移除字典中的最后一个元素,但字典本身没有排序,因此此方法在一个一个删除字典元素时候比较便捷
test={‘name’:‘Bill’,
‘usid’:‘9541’,
‘mark’:‘B’}
test.popitem()
print test
3.11 setdefault #和get类似,但是当键不存在时,setdefault会默认键值。
test={‘name’:‘Bill’,
‘usid’:‘9541’,
‘mark’:‘B’}
print test #{‘mark’: ‘B’, ‘name’: ‘Bill’, ‘usid’: ‘9541’}
a=test.setdefault(r’case’,‘Para or None’)
print a # Para or None
print test
#{‘case’:‘Para or None’,‘mark’:‘B’, ‘name’:‘Bill’, ‘usid’:‘9541’}