python基本数据类型列表、元组、字典、集合
2019-01-31 19:59
786 查看
文章目录
1、列表
lists = ['a','b','c'] print(lists) lists.append('d') print(lists) print(len(lists)) lists.insert(0,"mm") print(lists) lists.pop() #pop()删除尾部元素 print(lists)
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py ['a', 'b', 'c'] ['a', 'b', 'c', 'd'] 4 ['mm', 'a', 'b', 'c', 'd'] ['mm', 'a', 'b', 'c'] Process finished with exit code 0
2、元组
和列表相比缺点是一旦建之后就无法修改,无法插入删除,只可以访问
tuples = ('tupleA','tupleB') print(tuples[0])
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py tupleA Process finished with exit code 0
3、字典
key和values可以为任意类型
3.1 定义字典
score = {'guanyu':95,'zhangfei':96} print(score)
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py {'guanyu': 95, 'zhangfei': 96} Process finished with exit code 0
3.2 添加
score['zhaoyun'] = 98 print(score)
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py {'guanyu': 95, 'zhangfei': 96, 'zhaoyun': 98} Process finished with exit code 0
3.3 删除
score.pop('zhangfei') print(score)
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py {'guanyu': 95, 'zhaoyun': 98} Process finished with exit code 0
3.4 查询
#查看key是否存在 print('guanyu') in score
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py guanyu Process finished with exit code 0
#查询key对应的值 print(score.get('guanyu'))
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py 95 Process finished with exit code 0
3.5 修改
#当值不存在时赋一个值为99 print(score.get('yase',99))
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py 99 Process finished with exit code 0
3.6 遍历
3.6.1 遍历键和值
score = {'guanyu':95,'zhangfei':96} for name,grade in score.items(): print(name+':'+str(grade))
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py guanyu:95 zhangfei:96 Process finished with exit code 0
3.6.2 只遍历键
for name in score.keys(): print(name)
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py guanyu zhaoyun Process finished with exit code 0
3.6.3 只遍历值
for grade in score.values(): print(grade)
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py 95 98 Process finished with exit code 0
3.7 基于字典的模糊查找
模糊查找就是说输入一个key中的一部分,而非整个key,来进行相对应值的查找。
score = {'guanyu':95,'zhangfei':96,'zhaoyun':80,'songjiang':76} while (True): n=input("please enter the name of people:") for name,grade in score.items(): if name.startswith(n): print(name+':'+str(grade)) answer=input("wanna quit the game? y/n:") if answer=='y': break
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py please enter the name of people:g guanyu:95 wanna quit the game? y/n:y Process finished with exit code 0
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py please enter the name of people:song songjiang:76 wanna quit the game? y/n:n please enter the name of people:zha zhangfei:96 zhaoyun:80 wanna quit the game? y/n:y Process finished with exit code 0
参数介绍:
startswith(str, beg,end)
str – 检测的字符串。
beg – 可选参数用于设置字符串检测的起始位置。
end – 可选参数用于设置字符串检测的结束位置。
4、集合
和字典类似,但是它只有key,没有value,集合中不能包含重复元素,集合是无序的,不支持排序和索引
s = set(['a', 'b', 'c']) s.add('d') s.remove('b') print(s) print('c' in s)
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py {'c', 'a', 'd'} True Process finished with exit code 0
空集合的创建应该用set(),而不是{},因为{}创建出来的默认为一个空字典。
a=set() a.add('m') print(a)
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py {'m'} Process finished with exit code 0
求交并差集:
a={1,2,3,4} b={3,4,5,6} print("a和b的交集为"+str(a&b)) print("a和b的并集为"+str(a|b)) print("a和b的差集为"+str(a-b))
结果:
C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py a和b的交集为{3, 4} a和b的并集为{1, 2, 3, 4, 5, 6} a和b的差集为{1, 2} Process finished with exit code 0
相关文章推荐
- Python 学习 (一) python的基本数据类型 元组 列表 字典
- 【python】特色数据类型——元组(列表、元组、字典、集合)(6)
- 【python】特色数据类型——字典(列表、元组、字典、集合)(7)
- Python之几种重要的基本类型:元组,列表,字典,字符串,集合
- Python3组合数据类型(元组、列表、集合、字典)语法
- 基本数据类型-集合(set)_上周内容回顾(字符串_数字_列表_元组_字典_集合)
- python实操(5):python数据类型及常用操作,数字、字符串、元组、列表、字典、集合
- Python数据类型(元组、列表、字符串、字典)
- Python 入门学习 -----变量及基础类型(元组,列表,字典,集合)
- Python基础——原生数据类型(字典,列表,元组,字符串)
- python入门:基础,列表、元组、字典及集合类型
- Python学习笔记 - 基本数据结构:元组,列表,字典,集合
- Python 入门学习 -----变量及基础类型(元组,列表,字典,集合)
- python数据类型—列表、元组、字典
- python中的列表,元祖,字典,等基本数据类型的操作
- Python中基本数据存储结构列表、元组和字典
- Python基础:列表,元组,字典,集合相关基本操作
- Python基础【数据结构:列表 | 元组 | 集合 | 字典】
- python 数据类型 -列表-字典-元组-切片-内置方法
- 【八】列表、字典、集合、元组间的数据类型转换总结