20-Python3 数据结构

2018-11-30 15:45:55

'''
列表
'''
# list.count(x): 返回x在列表中出现的次数
a1 = [1,123.25,333,333,456.5]
print(a1.count(333),a1.count(123.25),a1.count(4)) # list.inster(i,x): 在索引号为i的元素前插入元素x
a2 =[2,123.25,333,333,456.5]
a2.insert(2,-1)
print('a2:',a2) # #a.insert(len(a),x):在列表的末尾添加x
a3 =[3,123.25,333,333,456.5]
a3.insert(len(a3),222)
print('a3:',a3) # #a.append(x):在列表末尾添加x
a4 =[3,123.25,333,333,456.5]
a4.append(222)
print('a4:',a4) #a.index(x):返回值为x的索引号
a5 =[3,123.25,333,333,456.5]
print('a5:',a5.index(3)) #a.remove(x):移除列表中的元素x
a6 =[3,123.25,333,333,456.5]
a6.remove(3)
print('a6:',a6) #a.reverse():列表中的元素倒排序
a7 =[3,123.25,333,333,456.5]
a7.reverse()
print('a7:',a7) #对列表中的元素进行排序
a8 =[3,123.25,333,333,456.5]
a8.sort()
print('a8:',a8) #list.pop():从列表中移除最后一个元素
a9 =[3,123.25,333,333,456.5]
a9.pop()
print('a9:',a9) #list.pop(i):从列表中移除索引为i的元素
a10 =[3,123.25,333,333,456.5]
a10.pop(1)
print('a10:',a10) '''
将列表当做对堆栈使用
'''
#后进先出
stack = [1,2,3,4,5,6]
stack.append(7)
print(stack)
stack.append(8)
print(stack)
stack.pop()
print(stack)
stack.pop()
print(stack) '''
将列表当作队列使用
'''
from collections import deque
queue = deque(['google','runoob','taobao'])
print('1:',queue)
queue.append('baidu')
queue.append('tianmao')
print('',queue) print(queue.popleft()) #从左边开始弹出,弹出后其他一个一个往前移动
print(queue.popleft()) print('3:',queue) '''
列表推导式
'''
#将列表的值都乘以3后输出
vec1 = [2,4,6]
vec12 = [3*x for x in vec1]
print('1:',vec12) #按要求生成子列表
vec2 = [2,4,6]
vec21 = [[x,x**2] for x in vec2]
print('2:',vec21) #对列表中对每个元素调用某方法
freshfruit = ['apple','banan','orange']
freshfruit1 = [weapon.strip() for weapon in freshfruit]
print('freshfruit1:',freshfruit1) #使用ifz作为过滤器
vec3 = [2,4,6]
vec31 = [3*x for x in vec3 if x >3]
print('vec31:',vec31) vec32 = [3*x for x in vec3 if x <5]
print('vec32:',vec32) #其他一些技巧演示
vec4 = [1,2,3]
vec5 = [4,5,-6]
vec451 = [x*y for x in vec4 for y in vec5]
print('vec451',vec451) vec452 = [x+y for x in vec4 for y in vec5]
print('vec452:',vec452) pass
pass '''
嵌套列表解析
'''
pass #del list[]:通过索引删除列表中的某个元素
d1 = [1,2,3,4,5,6,7]
del d1[0]
print('d1:',d1) #删除对元素个数4-2=2个,从开始对索引开始删除
d2 = [0,1,2,3,4,5,6,7]
del d2[2:4]
print('d2:',d2) #del list[:]:删除列表中对所有元素
d3 = [0,1,2,3,4,5,6,7]
del d3[:]
print('d3:',d3) #del d:删除一个实体变量
# d4 = [0,1,2,3,4,5,6,7]
# del d4
# print('d4:',d4) '''
元组和序列
'''
#元组对输出总是有圆括号对,输入可以有可以没有
t = (123,456,'hello')
print(t[0])
print('t:',t)
u = (t,(78,89,90))
print(u) #元组输入时可以没有圆括号
t1 = 1,2,3
print('t1',t1) '''
集合
'''
basket = {'apple','banana','orange','pear','apple','banana'} #字典的基本功能:消除重复元素
print(basket) #字典的基本功能:关系测试
print('apple' in basket)
print('runoob' in basket) #set():创建集合
a = set('abcdeabc')
b = set('cdefghrr') #集合去重操作
print(a) #在a中但不在b中的
print(a-b) #不同时在a或者b中
print(a^b) #a和b的并集
print(a|b) #a和b的交集
print(a&b) #集合也支持推导式
c = {x for x in a if x not in b}
print(c) '''
字典
'''
#使用{}创建字典
tel = {'jack01':6001,'jack02':6002,'jack03':6003} #向字典中添加元素
tel1 = {'jack01':6001,'jack02':6002,'jack03':6003}
tel1['jack04'] = 6004
print('tel1:',tel1) #del dict[键]:通过键值删除字典中的某个元素
tel2 = {'jack01':6001,'jack02':6002,'jack03':6003}
del tel2['jack01']
print('tel2:',tel2) #打印出字典的键值列表
tel3 = {'jack01':6001,'jack02':6002,'jack03':6003}
print('tel3:',list(tel3.keys())) #打印出字典的键值列表并排序
tel4 = {'jack01':6001,'ajack02':6002,'bjack03':6003}
print('tel4:',sorted(tel4.keys())) #关系测试
tel5 = {'jack01':6001,'ajack02':6002,'bjack03':6003}
print('tel51:','jack01' in tel5)
print('tel52:','jack02' in tel5) #构造函数dict()直接从键值队元=组列表中构建字典
tel6 = dict([('jack01',7001),('jack2',7002),('jack3',7003)])
print(tel6) #字典推导
dict1 = {x:x**2 for x in (2,4,6) }
print(dict1) #如果关键字只是简单对字符串,使用关键字参数指定键值对有时更方便
dict2 = dict(jack01=2001,jack02=2002,jack03=2003)
print(dict2) '''
遍历技巧
'''
#字典中遍历时,关键字和对值可以使用item()遍历出来
knights = {'jack01':'good','jack02':'better','jack03':'best'}
for k,v in knights.items():
print(k,v) #在遍历列表时,索引值和对应值可以使用enumerate()函数同时得到
for k,v in enumerate(['jack01','jack02','jack03']):
print(k,v) #zip()# 遍历两个或更多序列
questions=['name','age','class']
answers=['jack',18,'class1']
for q,a in zip(questions,answers):
print("'what's your{0}? It is {1}".format(q,a)) #反向遍历一个序列用reversed()
for i in reversed(range(1,10,2)):
print('反向遍历:',i) #sorted()按顺序遍历
str1 = ['a','b','d','e']
for i in sorted(str1):
print('sorted():',i)
上一篇:2020重新出发,MySql基础,MySql表数据操作


下一篇:SQL 实例 - RIGHT JOIN 关键字