python-输入数据处理与列表、元组、字典、random
2021/4/24 22:25:45
本文主要是介绍python-输入数据处理与列表、元组、字典、random,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
python-输入数据处理与列表、元组、字典
- 一、输入数据处理
- 形式一
- 形式二
- 二、字符串
- 三、列表
- 四、元组
- 五、字典
- 六、随机生成数-random
一、输入数据处理
在笔试的过程中遇到编程题我们经常遇到一些需要自己处理输入数据的情况,现在我将它们整理如下:
一般情况下我都会把输入数据处理存放在列表中。
形式一
""" 形式如下: 2 3 4 2,3,4 """ input_my =list(map(int,input().split())) input_my1 =list(map(int,input().split(',')))
运行结果如下所示:
形式一解释:
# 此解释针对python3 1、map()函数 s1 = map(x,y) s2 = list(map(x,y)) s1返回为 ‘迭代器’ s2返回为 ‘列表’;此列表是用x方法对y的各个元素进行处理然后返回存储在列表中 2、split()方法 s1 = ‘23 45 64’.split(‘x’,y) 首先表示以‘x’对字符串进行分割y次 其次s1返回为 ‘列表’,每个元素为字符 形式一就表示:首先对输入以'x'进行分割,并转化为int类型
形式二
""" 形式如下: 3 3 2 3 4 1 2 3 4 5 6 表示:输入3组数,每组3个数 """ total =list(map(int,input().split())) result = [] for i in range (0,total[0]): input_my =list(map(int,input().split())) if len(input_my) == total[1]: result.append(input_my) else: raise ValueError("本组输入数据与预设个数不符")
运行结果如下:
其他形式将持续更新。。。
二、字符串
字符串常见操作整理如下:
s1 = 'abcdefg-' s2 = '123456' # 字符串拼接 s = s1 + s2 # abcdefg-123456 # 字符串切片;[]左闭右开 # s:abcdefg-123456 res = s[1:5] # abcde res = s[len(s):7:-1] # 654321 res = s[::2] # aceg135 res = s[::-1] # 654321-gfedcba res = s.split('-') # ['abcdefg', '123456'] # 字符串运算 res = s * 2 # abcdefg-123456abcdefg-123456 res = s[0] # a # f-string w = {'name': '百度', 'url': 'www.baidu.com','port':'8080'} res = f'{w["name"]}: {w["url"]}:{w["port"]}' # 百度: www.baidu.com:8080 # 字符串内建函数 res = s.capitalize() #Abcdefg-123456 ;将第一个字母转化为大写 res = s.count('-') # 1 ;计数‘-’,出现的次数 res = s.endswith('-') # False res = s.endswith("6") # True ;判断是否以某结尾 res = s.startswith('a') # True res = s.startswith('b') # True ;判断是否以某开始 res = s.index('-') # 7 res = s.index('0') # 抛出异常 res = s.find('-') # 7 res = s.find('0') # -1 ;返回字符索引 res = s.isalnum() # False ;字符串是由字母或数字组成 res = s.isalpha() # False ;字符串是由字母组成 res = s.isdigit() # False ;字符串是由数字组成 res = s.isnumeric() # False ;字符串是由数字字符组成 res = s.isdecimal() # False """ isdigit() True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字 False: 汉字数字 Error: 无 isdecimal() True: Unicode数字,,全角数字(双字节) False: 罗马数字,汉字数字 Error: byte数字(单字节) isnumeric() True: Unicode数字,全角数字(双字节),罗马数字,汉字数字 False: 无 Error: byte数字(单字节) """ res = s.join('+++') # +abcdefg-123456+abcdefg-123456+ res = s.replace('-','+') # abcdefg+123456 res = s.rstrip() # abcdefg-123456 ;删除字符串末尾的空格
三、列表
列表常用操作整理如下:列表元素可变,可增、删、改
lst1 = [1,2,3,4] lst2 = ['a','b','c','d'] # 增加 # 1、append(1) lst = [0,0,0,0] lst.append("a") # [0,0,0,0,'a'] lst.append(lst1) # [0,0,0,0,'a', [1, 2, 3, 4]] # 2、insert() lst = [0,0,0,0] lst.insert(1,2) # [0, 2, 0, 0, 0] lst.insert(2,lst1) # [0, 2, [1, 2, 3, 4], 0, 0, 0] # 3、extend() lst = [0,0,0,0] lst.extend(1) # 异常 lst.extend('1') # [0, 0, 0, 0, '1'] lst.extend(lst1) # [0, 0, 0, 0, '1', 1, 2, 3, 4] lst = lst + lst2 # [0, 0, 0, 0, '1', 1, 2, 3, 4, 'a', 'b', 'c', 'd'] # 删除 lst = [1,2,3,4,5] lst.pop() # [1, 2, 3, 4] lst.remove(2) # [1, 3, 4] del lst[1] # [1, 4] lst.clear() # [] ;清空列表 # 修改 lst = [3,3,4,5,3,2,1] lst.sort() # [1, 2, 3, 3, 3, 4, 5] ;排序 lst.reverse() # [5, 4, 3, 3, 3, 2, 1] ;反转 # 查找 lst = [3,3,4,5,3,2,1] lst.index(3) # 0 ;返回第一个3出现的索引 lst.count(3) # 3 ;返回3的个数 # 其他 # 1、赋值 lst = [0,0,0,0] lst_new1 = lst # [0, 0, 0, 0] lst_new2 = lst.copy() # [0, 0, 0, 0] lst[2] = 10 lst_new1 # [0, 0, 10, 0] lst_new2 # [0, 0, 0, 0] print(lst_new1) print(lst_new2) # 2、切片 lst = [1, 1, 2, 2, 3, 5, 34] res = lst[2:len(lst):2] # [2, 3, 34] res = lst[1:] # [1, 2, 2, 3, 5, 34] res = lst[::-1] # [34, 5, 3, 2, 2, 1, 1] # 常用的字符串操作在列表中均适用
四、元组
元组常用操作整理如下:元组元素不变,不可增、删、改
# 创建字典若元素只有一个时,为不引起歧义,必须在其后加上逗号 tup = (10,) # 查找 tup = (2,3,4,32,2,2,2,4,5,6,7,5) res = tup.index(2) # 0 res = tup.count(2) # 4 # 切片 tup = tup[0:3] # (2, 3, 4) tup = tup[::-1] # (5, 7, 6, 5, 4, 2, 2, 2, 32, 4, 3, 2) ## 利用切片和构造新元组实现元组的增、删、改 ## 首先它可以'+' tup1 = (1,2,3,4) tup2 = ('a','b','c') tup = tup1 + tup2 # (1, 2, 3, 4, 'a', 'b', 'c') tup = (1,2,3,4,5,6) ### 增加,在索引为2的位置加入 99 tup = tup[:2] + (99,) + tup[2:] # (1, 2, 99, 3, 4, 5, 6) ### 修改,将索引为2的数字改为 99 tup = tup[:2] + (99,) + tup[3:] # (1, 2, 99, 4, 5, 6) ### 删除,将索引为3的数字删除 tup = tup[:3] + tup[4:] # (1, 2, 3, 5, 6) del tup # NameError: name 'tup' is not defined ;删除元组
五、字典
字典常用操作整理如下:
# 字典 # 创建字典 student1 = {'clas':'计科18','name':'小红'} # {'clas': '计科18', 'name': '小红'} student2 = dict( clas ='信息18', name = '小明') # {'clas': '信息18', 'name': 'xiaoming '} student = {'clas':'计科18','name':'小红'} # 修改字典 student['clas'] = '信息18' # {'clas': '信息18', 'name': '小红'} # 添加字典 student['id'] = '001' # {'clas': '信息18', 'name': '小红', 'id': '001'} # 删除字典 del student['clas'] # {'name': '小红', 'id': '001'} del student # NameError: name 'student' is not defined ;删除字典 # ·遍历字典 student = {'clas': '信息18', 'name': '小红', 'id': '001'} for key,value in student.items(): print(key,':',value) """ 输出为: clas : 信息18 name : 小红 id : 001 """ for key in student.keys(): # clas-name-id- print(key,end='-') for value in student.values(): # 信息18-小红-001- print(value,end = '-') # ·字典的嵌套 ## 字典嵌套进列表 student1 = {'clas':'计科18','name':'小红','id':'001'} student2 = {'clas':'计科18','name':'小黑','id':'002'} student3 = {'clas':'计科18','name':'小白','id':'003'} student = [student1,student2,student3] # [{'clas': '计科18', 'name': '小红', 'id': '001'}, {'clas': '计科18', 'name': '小黑', 'id': '002'}, {'clas': '计科18', 'name': '小白', 'id': '003'}] ## 列表嵌套进字典 student_name = ['小红','小黑','小白'] student_clas = ['计科18','计科18','计科18'] student_id = ['001','002','003'] student = {'name':student_name,'clas':student_clas,'id':student_id} # {'name': ['小红', '小黑', '小白'], 'clas': ['计科18', '计科18', '计科18'], 'id': ['001', '002', '003']} student['name'][1] # 小黑 ## 字典嵌套进字典 student1 = {'clas':'计科18','name':'小红','id':'001'} student2 = {'clas':'计科18','name':'小黑','id':'002'} student3 = {'clas':'计科18','name':'小白','id':'003'} student = {'information1':student1,'information2':student2,'information3':student3} # {'information1': {'clas': '计科18', 'name': '小红', 'id': '001'}, 'information2': {'clas': '计科18', 'name': '小黑', 'id': '002'}, 'information3': {'clas': '计科18', 'name': '小白', 'id': '003'}} student['information1']['name'] # 小红
六、随机生成数-random
random模块常用随机生成数字整理如下:
import random # 随机生成1-10的浮点数 res = random.uniform(1,10) # 2.2597718045640427 # 随机生成1-10的整数 res = random.randint(1,10) # 9 # 随机生成1-9的整数 res = random.randrange(1,10) # 5 # 随机选择一个数 res = random.choice([1,2]) # 0 # 随机选择多个数 res = random.choices([1,2,3,4,5],weights=[0.1,0.2,0.3,0.5,0.4],k=3) # [4, 5, 2] # weights是权重,k是次数,可不加权重 res = random.choices([1,2,3,4,5],k=3) # [3, 2, 3] print(res)
这篇关于python-输入数据处理与列表、元组、字典、random的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python