python 列表、元组、字符串、字典、集合、return等梳理

有必要对这些数据类型及操作做下梳理:

1.列表:增删改查  

a.查找:

 >>> names=["zhang","wang","li","zhao"]
#列表天生具有下标,基于下标0,1,2,...进行查找
>>> names[1]
'wang'
#列表的切片,即也是基于下标进行操作
>>> names[1:3]
['wang', 'li']
>>> names[-1:]
['zhao']
>>> names[:3]
['zhang', 'wang', 'li']
>>> names[1:]
['wang', 'li', 'zhao']
#基于列表中的值找下标
>>> print(names.index('li'))
2
>>> print(names[names.index('li')])
li
#基于for循环查找列表值
>>> for i in names:
... print(i)
...
zhang
wang
li
zhao

b.修改

 >>> names=["zhang","wang","li","zhao"]
>>> names.append("Sun")
>>> names
['zhang', 'wang', 'li', 'zhao', 'Sun']
>>> names.insert(1,'qian')
>>> names
['zhang', 'qian', 'wang', 'li', 'zhao', 'Sun']
>>> names[2] = 'gao'
>>> names
['zhang', 'qian', 'gao', 'li', 'zhao', 'Sun']

c.删除

 >>> names
['zhang', 'qian', 'gao', 'li', 'zhao', 'Sun']
>>> names.remove('zhang')
>>> names
['qian', 'gao', 'li', 'zhao', 'Sun']
>>> del names[1]
>>> names
['qian', 'li', 'zhao', 'Sun']
>>> names.pop()
'Sun'
>>> names.pop(1)
'li'
>>> names
['qian', 'zhao']

d.增加

 >>> names
['qian', 'zhao']
>>> names.append('Sun') #追加
>>> names
['qian', 'zhao', 'Sun']
>>> names.insert(1,'zhang') #插入
>>> names
['qian', 'zhang', 'zhao', 'Sun']
>>> names2=[1,2,3,4]
>>> names.extend(names2) #合并
>>> names
['qian', 'zhang', 'zhao', 'Sun', 1, 2, 3, 4]
#深浅copy
>>> names3 = names.copy()#浅copy,指向同一块内存地址
>>> names
['qian', 'zhang', 'zhao', 'Sun', 1, 2, 3, 4]
>>> names3
['qian', 'zhang', 'zhao', 'Sun', 1, 2, 3, 4]
#深copy,新增一块内存地址,把数据完全copy一份到新内存
>>> import copy
>>> names
['qian', 'zhang', 'zhao', 'Sun', 1, 2, 3, 4]
>>> names4 = copy.deepcopy(names)
>>> names4
['qian', 'zhang', 'zhao', 'Sun', 1, 2, 3, 4]

2.元组:只能查询和切片,也叫只读列表,只有count和index方法

 >>> names = ('alex','wang','eric')
>>> names.append('li')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
>>> names[1]
'wang' >>> print(names.index('alex'))
0
>>> print(names[names.index('alex')])
alex

3.字符串:文件中只能存储字符串,且不可修改

 name="my name is alex"
print(name.center(50,"-")) 打印50个字符,不够用-补上,并把name放中间
-----------------my name is alex------------------
>>> print(name.startswith('my')) #判断是否以某字符开头
True
print(name.endswith("ex")) 判断name字符串是否以ex结尾
My name is alex name="my\tnameis{name}andmyageis{year}old"
print(name.format(name='alex',year=23))
my name is alex and my age is 23 old
print(name.index('a'))
5
print(''.isdigit())是否为整数
True
print('+'.join(['','','',''])) 经常用
1+2+3+4
print('alexli'.replace('l','L')) 把小写l替换成大写的L
aLex Li
print('alex li'.split())把字符串按空格组成一个列表
['alex', 'li']

4.字典:key:value的数据类型,无序

>>> info = {'stu1101':'wang','stu1102':'zhang','stu1103':'li'}

a.增加

 >>> info = {'stu1101':'wang','stu1102':'zhang','stu1103':'li'}
>>> info['stu1104'] = 'zhao'
>>> info
{'stu1101': 'wang', 'stu1102': 'zhang', 'stu1103': 'li', 'stu1104': 'zhao'} #合并
>>> info2 = {1:3,2:5}
>>> info.update(info2)
>>> info
{'stu1101': 'wang', 'stu1102': 'zhang', 'stu1103': 'li', 'stu1104': 'zhao', 1:
, 2: 5}

b.修改

 >>> info = {'stu1101':'wang','stu1102':'zhang','stu1103':'li'}
>>> info['stu1101'] = 'lu'
>>> info
{'stu1101': 'lu', 'stu1102': 'zhang', 'stu1103': 'li'}

c.删除

 >>> info
{'stu1101': 'lu', 'stu1102': 'zhang', 'stu1103': 'li'}
>>> del info['stu1101']
>>> info
{'stu1102': 'zhang', 'stu1103': 'li'} info.pop("stu1101") #pop删除
info.popitem()随机删除

d.查询

 >>> info
{'stu1102': 'zhang', 'stu1103': 'li'}
>>> info['stu1102']
'zhang'
#判断是否存在字典中
print(info.get('stu1104')) 安全获取的方法
None
>>> print('stu1103' in info)
True >>> info
{'stu1102': 'zhang', 'stu1103': 'li'}
>>> info['stu1102']
'zhang'
>>> for i in info: #最建议的查询方式
... print(i,info[i])
...
stu1102 zhang
stu1103 li
>>> for k,v in info.items():
... print(k,v)
...
stu1102 zhang
stu1103 li >>> print(info.items())
dict_items([('stu1102', 'zhang'), ('stu1103', 'li')])

e.多级字典的查询方式

python 列表、元组、字符串、字典、集合、return等梳理

5.集合:无序,天生去重

 >>> list = [1,2,3,4]
>>> list_1 = set(list)
>>> print(type(list_1),list_1)
<class 'set'> {1, 2, 3, 4}

关系型测试:

交差并子父对称集操作

a.增加

 >>> list = [1,2,3,4]
>>> list_1 = set(list)
>>> print(type(list_1),list_1)
<class 'set'> {1, 2, 3, 4}
>>> list_1.add(999)
>>> list_1
{1, 2, 3, 4, 999}
>>> list_1.update([777,888])
>>> list_1
{1, 2, 3, 4, 999, 777, 888}

b.删除

 >>> list_1.remove(999)
>>> list_1
{1, 2, 3, 4, 777, 888}
>>> list_1.discard(888)
>>> list_1
{1, 2, 3, 4, 777}
>>> list_1.pop()
1

c.深浅复制,跟列表一样

d.判断

len(list_1)  #测试长度

判断是否存在集合中,大于小于等

6.return

两个作用:

1.得到程序的执行结果;

2.程序结束,return后的代码不再执行;

python 列表、元组、字符串、字典、集合、return等梳理

上一篇:Node.js入门:Node.js&NPM的安装与配置


下一篇:node.js&pm2搭建node生产环境