python3 列表的查看修改和排序,元组、集合以及字典的相关介绍
python是面向对象的语言,胶水语言,psutil,系统参数监控。pv一个页面一个pv,uv一个用户一个uv。
列表修改。
1、通过索引、重新赋值。
service = [‘http’,‘samba’,‘nfs’] ####给列表给值
service[0]= ‘mysql’ ###索引,更换http
print(service) ##输出
[‘mysql’, ‘samba’, ‘nfs’] ##更换完毕
2、通过切片。
service = [‘http’,‘samba’,‘nfs’]
service[0]= ‘mysql’
print(service)
print(service[:2]) ##用切片查看前两位
service[:2]=[‘firewalld’,‘iscsi’] ##用切片更换前两位
print(service)
[‘mysql’, ‘samba’, ‘nfs’]
[‘mysql’, ‘samba’]
[‘firewalld’, ‘iscsi’, ‘nfs’]
3、列表查看
1、查看次数,索引值,指定索引范围。 service = ['http','samba','nfs','iscsi','http'] ####列表中的值 print(service.count('http')) ##列表中http有几个 print(service.index('http')) ##列表中http的索引是第机为 print(service.index('http',2,5)) ##列表中索引第2为到第5位(左闭右开) 2 0 4
4\排序
service = ['http', 'samba', 'nfs', 'iscsi', 'http'] service.sort() ##排序 print(service) ##输出 import random ##给一个随几数库 li = list(range(10)) ##给一个10的范围列表 print(li) ##打印它 random.shuffle(li) ##让其乱序排序 print(li) ## 打印 ['http', 'http', 'iscsi', 'nfs', 'samba'] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 9, 2, 6, 3, 8, 5, 7, 0, 4]
############用户添加到列表中去
print('管理员登陆'.center(50, '*')) aduser = input('username:') ##输入用户名 adpasswd = input('password:') ##密码 users = ['root', 'redhat'] ##用户名列表 passwd = ['123', '456'] ##密码列表 if aduser == 'admin' and adpasswd == 'westos': ##如果是admin 和westos登陆成功 print('管理员登陆成功!') print('用户登陆'.center(50,'*')) while True: ##登陆成功后进行循环,正确循环 print(""" 菜单 1.添加用户信息 2.删除用户信息 3.查看用户信息 4.退出 """) choice = input('请输入选择:') ##输入数字 if choice == '1': ##选择1 print('添加用户信息'.center(50, '*')) adduser = input('添加用户名') if adduser in users: ##用户在里面 print('用户{adduser}已经纯在') else: ##用户不再里面 addpasswd = input('密码:') ##分别添加 users.append(adduser) passwd.append(addpasswd) print(f'用户{adduser}添加成功') elif choice =='2': ##选择2 print('删除用户信息'.center(50,'*')) deluser = input('删除用户名') ##输入要删除的用户名 if deluser in users: ##如过输入在用户列表中 delindex = users.index(deluser) ##分别移除用户名和密码 users.remove(deluser) passwd.pop(delindex) print(f'删除用户{deluser}成功') else: ##删除的用户不存在时 print(f"用户{deluser}不存在") elif choice =='3': ##选择3 print('查看用户信息'.center(50,'*')) print('\t用户名\t密码') userlenth = len(users) ##通过一个一个字符显示的方式显示用户名和密码 for i in range(userlenth): print('\t%s\t%s' %(users[i],passwd[i])) elif choice =='4': ##选择4 exit() ##推出 else: ##其它 print('请输入正确的选择') else: ##不是admin时 print('管理员登陆失败')
########################栈的功能,实现
print('管理员登陆'.center(50, '*')) aduser = input('username:') adpasswd = input('password:') users = [] if aduser == 'admin' and adpasswd == 'westos': print('管理员登陆成功!') print('用户登陆'.center(50,'*')) while True: print(""" 菜单 1.入栈 2.出栈 3.栈顶元素 4.栈的长度 5,栈是否为空 """) choice = input('请输入选择:') if choice == '1': print('入栈'.center(50, '*')) adduser = input('名称') if adduser in users: print('已经存在') else: users.append(adduser) print(f'用户{adduser}添加成功') elif choice =='2': print('出栈'.center(50,'*')) if users[-1] in users: del users[-1] print(f'出栈成功') else: print(f"不存在") elif choice =='3': print('栈顶元素'.center(50,'*')) print('\t栈顶元素') if users==[]: print('为空') print(users[-1]) # elif choice =='4': userlenth = len(users) print(userlenth) elif choice =='5': if users == []: print('为空') else: print('不为空') else: print('请输入正确的选择') else: print('登陆失败')
###################栈的内置方法
a = min(2,3,4,5,) ###最小值 print(a) a = max(2,3,4,5) ##最大值 print(a) a = sum(range(1,101)) ##求和 print(a) a = sum(range(1,101,2)) ##质数求和 print(a) a = sum(range(2,101,2)) ##偶数求和 print(a) ##结果 2 5 5050 ##质数+偶数=总数 2500 2550
#枚举:返回索引值和对应的value值,是将一个字符串拆分显示
for i in enumerate('westos'): ##遍历字符串,用一个 print(i) for i,v in enumerate('westos'): ##用两个遍历 print(i,v) ##结果 (0, 'w') (1, 'e') (2, 's') (3, 't') (4, 'o') (5, 's') 0 w 1 e 2 s 3 t 4 o 5 s
############将两个字符串这样枚举
s1 = 'abc' s2 = '123' for i in zip(s1,s2): ##将两个字符串枚举 print(' '.join(i)) ##用相连的方法 a 1 b 2 c 3
###########元组
t = (2,3,4,'httpd',True) ##元组可以存储任意数据类型 print(t,type(t)) t1 = ([1,2,3],5) ##元组里包含可变数据类型,可以间接修改元组内容 t1[0].append(4) print(t1) t2=('hello') ##元组如果只有一个元素的时候 print(t2,type(t2)) t2 =('hello',) print(t2,type(t2)) (2, 3, 4, 'httpd', True) <class 'tuple'> ([1, 2, 3, 4], 5) hello <class 'str'> ('hello',) <class 'tuple'>
#######################元组的特性
#索引 切片
print(users[0]) ##0位 print(users[-1]) ##最后一位 print(users[1:]) ##除过0位 print(users[:2]) ##前两位 print(users[::-1]) ##逆序 root redhat ('westos', 'redhat') ('root', 'westos') ('redhat', 'westos', 'root')
#连接、重复、成员操作符
users = ('root','westos','redhat') passwds = ('123','456','789') print(users * 3) #重复 print(users + ('rhce','rhcsa')) #连接 print('westos' in users) #成员操作符 print('westos' not in users) ('root', 'westos', 'redhat', 'root', 'westos', 'redhat', 'root', 'westos', 'redhat') ('root', 'westos', 'redhat', 'rhce', 'rhcsa') True False
#元组的枚举、循环、计数、枚举号
users = ('root','westos','redhat') passwds = ('123','456','789') for user in users: #for循环 print(user) for index,user in enumerate(users): ##枚举users print(index,'-->',user) for user,passwd in zip(users,passwds): ##将密码和用户分别压缩枚举 print(user,':',passwd) t = (1,1.2,True,'westos') ##重新定义一个元组 print(t.count('westos')) ##元组看有几个westos print(t.index('westos')) ##查看字符串的索引号 结果: root westos redhat 0 --> root 1 --> westos 2 --> redhat root : 123 westos : 456 redhat : 789 1 3
#############元组的应用
a = 1 ##付值 b = 2 (a,b) = (2,1) ##直接永远组给ab值 print(a,b) a = 1 b = 2 b, a = a, b ##用这种方法交换ab值 print(a, b) b=(1,2)[1] ##用索引的方法给值 a=(1,2)[0] print(a, b) ##结果 2 1 2 1 1 2
##元组的排序
scores = (100,89,45,78,65) scoreli = list(scores) ##转换位列表 scoreli.sort() ##用sort排序 print(scoreli) scores = sorted(scores) ##直接用sorted进行元组排序 print(scores) minscore,*middlescore,maxscore = scores ##利用元组将最大值,最小值,中间值区分开来 print(minscore) print(middlescore) print(maxscore) print('最终成绩为: %.2f' %(sum(middlescore) / len(middlescore))) ##用sum进行求中间值的和,用len来求中间值的个数。两者相除求平均值 ##结果 [45, 65, 78, 89, 100] [45, 65, 78, 89, 100] 45 [65, 78, 89] 100 最终成绩为: 77.33
##集合定义
s = {1,2,3,1,2,3,4,5} #集合里面的元素是不可重复的 print(s) print(type(s)) s3 = set([]) #如何定义一个空集合 print(s3) print(type(s3)) li = [1,2,3,1,2,3] #集合应用,去除重复的 print(list(set(li))) ##结果 {1, 2, 3, 4, 5} <class 'set'> set() <class 'set'> [1, 2, 3]
##集合特性
s = {1,2,3} #集合只支持成员操作符号 for循环 print(1 in s) #成员操作符 for i in s: #for循环 print(i) True 1 2 3
##常用方法
s = {6, 7, 8, 9} s.add(1) #增加一个元素 print(s) s.update({5,3,2}) #增加多个 print(s) s.pop() #删除最后一个 print(s) s.remove(6) ##删除具体的哪个元素 print(s) {1, 6, 7, 8, 9} {1, 2, 3, 5, 6, 7, 8, 9} {2, 3, 5, 6, 7, 8, 9} {2, 3, 5, 7, 8, 9}
##常用方法2
s1 = {1,2,3} #交集 并集 差集 s2 = {2,3,4} print('并集:',s1.union(s2)) #并集 print('并集:',s1 | s2) print('交集:',s1.intersection(s2)) #交集 print('交集:',s1 & s2) print('差集:',s1.difference(s2)) #s1 - (s1 & s2) print('差集:',s2.difference(s1)) #差集 #结果 并集: {1, 2, 3, 4} 并集: {1, 2, 3, 4} 交集: {2, 3} 交集: {2, 3} 差集: {1} 差集: {4}
##父级、子集
s3 = {1,2} s4 = {1,2,3} print(s4.issuperset(s3)) ##s4是s3的父集 print(s3.issubset(s4)) ##s3是s4的子集 print(s3.isdisjoint(s4) ##s3没有在s4中 ##结果 True True False
华为机测题:
明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1~1000之间的随机整数(N<=1000),N是用户输入的,对于其中重复的数字,只保留一个,把其余相同的数字去掉,不同的数对应,着不同的学生的学号,然后再把这些,数从小到大排序,按照排好的顺序去找同学做调查,请你协助明明完成,“去重”与排序工作。
import random ##用集合来消除指定重复数 s = set([]) ##定义空集 for i in range(int(input('N: '))): ##用户输入数字 s.add(random.randint(1,1000)) ##将其添加到随机数中 print(sorted(s)) ##输出排序之后的情况
######################################字典
users = ['user1','user2'] passwds = ['123','456'] print(dict(zip(users,passwds))) ##字典是一个无序的数据集合 d = {} ##空字典的定义 print(type(d)) s = { ##字典中的数据可以任意 'westos':[100,99,88], 'redhat':[50,60,70] } print(s,type(s)) #结果 {'user1': '123', 'user2': '456'} <class 'dict'> {'westos': [100, 99, 88], 'redhat': [50, 60, 70]} <class 'dict'>
##字典
d = {} print(type(d)) d = dict() #工厂函数 print(d) print(type(d)) d = dict(a=1,b=2) ##工厂函数的字典付值 print(d) print(type(d)) a = { #字典的嵌套 '03113009':{ 'name':'wsp', 'age':18, 'score':90 }, '12345678':{ 'name':'laoli', 'age':36, 'score':80 } } print(a) ##输出 #结果 <class 'dict'> {} <class 'dict'> {'a': 1, 'b': 2} <class 'dict'> {'03113009': {'name': 'wsp', 'age': 18, 'score': 90}, '12345678': {'name': 'laoli', 'age': 36, 'score': 80}}
print({}.fromkeys({'1','2'},'000000')) ##给两个key一个value 结果: {'2': '000000', '1': '000000'}
########字典的特性
d = dict(a=1,b=2) print('a' in d) ###成员操作符 print('a' not in d) for k in d: ###for循环 print(k,d[k]) ##d【k】表示value值 True False a 1 b 2
########字典的增加
services = {'http':80,'ftp':21,'mysql':3306} #增加一个元素 services['ssh'] = 22 #如果key值不存在,添加key-value值对 print(services) services['http'] = 443 #如果key值存在,则更新对应的value值 print(services) services_backup = {'https':443,'tomcat':8080,'ssh':22} services.update(services_backup) #添加多个key-value值 print(services) services.update(flask=9000,http=8000) print(services) ##结果 {'http': 80, 'ftp': 21, 'mysql': 3306, 'ssh': 22} {'http': 443, 'ftp': 21, 'mysql': 3306, 'ssh': 22} {'http': 443, 'ftp': 21, 'mysql': 3306, 'ssh': 22, 'https': 443, 'tomcat': 8080} {'http': 8000, 'ftp': 21, 'mysql': 3306, 'ssh': 22, 'https': 443, 'tomcat': 8080, 'flask': 9000}
services = { 'http':80,'ftp':21,'mysql':3306} #setdefault添加key值 services.setdefault('http',9090) #如果key值存在,不做修改 print(services) services.setdefault('oracle',44575)#如果key值不存在,添加对应的key-value print(services) ##结果 {'http': 80, 'ftp': 21, 'mysql': 3306} {'http': 80, 'ftp': 21, 'mysql': 3306, 'oracle': 44575}
######字典的删除
services = {'http':80,'ftp':21,'mysql':3306,'ssh':8080} del services['http'] print(services) item = services.pop('mysql') #pop删除指定的key的key-value print(item) #如果key存在,删除,并返回删除key对应的value print(services) item = services.popitem() #popitem删除最后一个key-value值 print(item) print(services) services.clear() #清空字典内容 print(services) ##结果 {'ftp': 21, 'mysql': 3306, 'ssh': 8080} 3306 {'ftp': 21, 'ssh': 8080} ('ssh', 8080) {'ftp': 21} {}
##查看
services = {'http':80,'ftp':21,'mysql':3306} print(services.keys()) #查看字典的key值 print(services.values()) #查看字典的value值 print(services.items()) #查看字典的key-value值对 print(services['http']) #查看key的value值 #key不存在,默认返回None,有default,则返回default print(services.get('http')) #如果key值不存在,默认返回None,如果需要指定返回值,传值即可 print(services.get('https','key not exist')) ##结果 dict_keys(['http', 'ftp', 'mysql']) dict_values([80, 21, 3306]) dict_items([('http', 80), ('ftp', 21), ('mysql', 3306)]) 80 80 key not exis
- 点赞
- 收藏
- 分享
- 文章举报
- python 列表list 元组tuple 字典dict 集合set 介绍
- Python基础:列表,元组,字典,集合相关基本操作
- python 列表list 元组tuple 字典dict 集合set 介绍
- Python中的列表,元组,字典以及集合的概念
- Python学习笔记(9)-列表list、元组tuple、字典dict、集合set
- python笔记2 (列表,元组,字典,集合)
- python中列表 元组 字典 集合的区别
- Python中列表、字典、元组、集合数据结构整理
- Python基础之列表、元组、字典、集合的使用
- Python学习之---集合,元组,列表,字符串,字典 ---组合数据类型---使用方法
- Python数据结构:列表、字典、元组、集合
- 【python】特色数据类型——元组(列表、元组、字典、集合)(6)
- 20170214--PYTHON介绍+(列表,字典,元组)
- **python中列表 元组 字典 集合
- 第七节:python列表、元组、字典、集合
- python列表、元组、字典的介绍及常用方法
- python 列表,元组,字典,集合简单总结
- python基础(5)---整型、字符串、列表、元组、字典内置方法和文件操作介绍
- Python进阶(九):列表,元组,字典,集合的同异
- 1900页Python系列PPT分享二:Python序列(列表、元组、字典、集合)(154页)