字符串常用功能和列表元组的基本操作
字符串的常用功能:
1.移除空白
2.分割
3.长度
4.索引
5.切片
例:
1.移除空白,在代码后面加入.strip(),也可以在括号里添加想要去除的代码
name = input("name:").strip()
age = int(input("age:"))
job = input("job:")
msg='''Infomation of %s:
name: %s
age: %d
job: %s
'''%(name,name,age,job)
print(msg)
name: panzhonglv
age: 22
job: it
Infomation of panzhonglv:
name: panzhonglv
age: 22
job: it
列表的基本操作:
dir(name_list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append'(追加), 'clear'(清空列表), 'copy'(拷贝), 'count'((统计)), 'extend'(扩展), 'index'(索引), 'insert'(插入), 'pop'(删除), 'remove'(删除指定的一个), 'reverse'(反转), 'sort'(排序)]
>>> name_list = ['alex','panzl','chaoge','aoyun']
>>> name_list
['alex', 'panzl', 'chaoge', 'aoyun']
取出'panzl',在取出'aoyun',在取出'alex'
>>> name_list[1]
'panzl'
>>> name_list[3]
'aoyun'
>>> name_list[0]
'alex'
列表相对于变量的好处:可以存多个信息,变量一般只存一个字符串一个数字,信息量有限,通过列表可以存多个,变量虽然可以存多个变量,但是不能单独取出,无法调用
PYTHON中每个元素都会对应一个编号,也就是下标,当在列表中取值时,通过对应的下标,就可以取出来。
追加 append
例:
>>> name_list.append('hongwei')
>>> name_list
['alex', 'panzl', 'chaoge', 'aoyun', 'hongwei']
>>> name_list.append('panzl') #还可以添加重名
>>> name_list
['alex', 'panzl', 'chaoge', 'aoyun', 'hongwei', 'panzl']
索引
把列表中panzl取出来,很简单,但当列表有5000个,甚至更多是,就需要找到对应的索引值
比如我们要找'panzl'的索引值时就可以:
>>> name_list.index('panzl')
1
找到了,index就会显示最先找到的那个也就是1,但是它只返回了一个,但后面还有一个,那这时就要通过count来统计
>>> name_list.count('panzl')
2
插入 insert
比如在'panzl'后面插入另一个字符串
例如:
>>> name_list.insert(2,'jibw')
>>> name_list
['alex', 'panzl', 'jibw', 'chaoge', 'aoyun', 'hongwei', 'panzl']
删除
pop是从最后一个开始删除
>>> name_list
['alex', 'panzl', 'jibw', 'chaoge', 'aoyun', 'hongwei', 'panzl']
>>> name_list.pop()
'panzl'
>>> name_list
['alex', 'panzl', 'jibw', 'chaoge', 'aoyun', 'hongwei']
li = list([1,2,3])
print(li)
ret = li.pop(0)
print(li)
print(ret)
结果
[1, 2, 3]
[2, 3]
1
remove是指定删除一个
>>> name_list.remove('jibw')
>>> name_list
['alex', 'panzl', 'chaoge', 'aoyun', 'hongwei']
>>>
如何把指定删除多个相同的元素,比如删除'aoyun'
>>> name_list.insert(5,'aoyun')
>>> name_list.insert(2,'aoyun')
>>> name_list
['alex', 'aoyun', 'aoyun', 'chaoge', 'hongwei', 'panzl', 'aoyun']
>>> for i in range(name_list.count('aoyun')):
name_list.remove('aoyun')
反转 reverse
>>> name_list
['alex', 'panzl', 'chaoge', 'aoyun', 'hongwei']
>>> name_list.reverse()
>>> name_list
['hongwei', 'aoyun', 'chaoge', 'panzl', 'alex']
sort 排序,根据ASCLL码来排序的
>>> name_list
['hongwei', 'aoyun', 'chaoge', 'panzl', 'alex']
>>> name_list.sort()
>>> name_list
['alex', 'aoyun', 'chaoge', 'hongwei', 'panzl']
切片
>>> a = [1,2,3,'a','b']
>>> a
[1, 2, 3, 'a', 'b']
>>> a.sort()
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
a.sort()
AttributeError: 'tuple' object has no attribute 'sort'
>>> a.insert(1,6)
>>> a
[1, 6, 2, 3, 'a', 'b']
>>> a[0:2] #顾首不顾尾
[1, 6]
>>> a.insert(3,4)
>>> a
[1, 6, 2, 4, 3, 'a', 'b']
>>> a[1:5:3] #跳着切片
[6, 3]
>>> a[-2:] #倒着切片
['a', 'b']
>>> b = a[0:5] #在python3中sort排序数字和字符串一起会报错,我们可以用切片方式分开
>>> b
[1, 6, 2, 4, 3]
>>> b.sort()
>>> b
[1, 2, 3, 4, 6]
长度
extend 扩展列表
>>> a.extend(b)
>>> a
[1, 6, 2, 4, 3, 'a', 'b', 1, 2, 3, 4, 6]
循环
包含
>>> a = [1,6,2,4,3,'a','b',1,2,3,4,6]
>>> a
[1, 6, 2, 4, 3, 'a', 'b', 1, 2, 3, 4, 6]
>>> 4 in a
True #判断4是否在a中,True就为真
元组 (元组能做的事,列表都能做,实际意义在于希望别人不要改)
>>> t = (1,2,3,4)
(1, 2, 3, 4)
>>> dir(t)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
元组和列表之间的转换
>>> type(t)
<class 'tuple'>
>>> list(t)
[1, 2, 3, 4]
>>> tuple(a)
(1, 6, 2, 4, 3, 'a', 'b', 1, 2, 3, 4, 6)
>>> list(a)
[1, 6, 2, 4, 3, 'a', 'b', 1, 2, 3, 4, 6]