1、从输入的字典中获取给定key的值,
实现函数:get_key_value(source_dict, key, index=0),source_dict指给定的字典,key指需要获取的key字段,index指的是需要获取相同key的第几个值,从0开始,默认是0

"""
File: 01dict取值.py
Author: chde_wang
Date: 2020-07-29 22:48:33
Description:从输入的字典中获取给定key的值,实现函数:get_key_value(source_dict, key, index=0),source_dict指给定的字典,key指需要获取的key字段,index指的是需要获取相同key的第几个值,从0开始,默认是0。e.g: get_key_value(a, ‘id’, 2),返回值应该是:7554611
"""
a = {"code":200,"msg":"success","data":{"list":{"pay_column":[],"column":[{"id":6443650,"app_id":1561979372,"username":"qfhyadx","alias":"","title":"\u5de5\u5177","desc":"","img_url":"https:\/\/img-blog.csdnimg.cn\/20190927151132530.png","channel_id":0,"sum":16,"sort":6443650,"is_show":1,"status":1,"is_free":0,"is_delete":2,"status_count":"{\"1\":16}","created_at":"2019-08-26T04:19:51Z","updated_at":"2019-11-22T07:53:45Z","second":False,"edit_title":"\u5de5\u5177","column_subscribe_count":0,"column_url":"https:\/\/blog.csdn.net\/qfhyadx\/category_6443650.html","is_audit_pending":False},{"id":6459549,"app_id":1561979372,"username":"qfhyadx","alias":"","title":"\u968f\u7b14","desc":"","img_url":"https:\/\/img-blog.csdnimg.cn\/20190927151043371.png","channel_id":0,"sum":4,"sort":6459549,"is_show":1,"status":1,"is_free":0,"is_delete":2,"status_count":"{\"1\":4}","created_at":"2019-08-26T04:19:51Z","updated_at":"2020-07-19T21:49:55Z","second":False,"edit_title":"\u968f\u7b14","column_subscribe_count":0,"column_url":"https:\/\/blog.csdn.net\/qfhyadx\/category_6459549.html","is_audit_pending":False},{"id":7554611,"app_id":1561979372,"username":"qfhyadx","alias":"","title":"python3","desc":"","img_url":"https:\/\/img-blog.csdnimg.cn\/20190927151101105.png","channel_id":0,"sum":2,"sort":7554611,"is_show":1,"status":1,"is_free":0,"is_delete":2,"status_count":"{\"1\":2}","created_at":"2019-08-26T04:20:06Z","updated_at":"2020-07-13T19:41:03Z","second":False,"edit_title":"python3","column_subscribe_count":0,"column_url":"https:\/\/blog.csdn.net\/qfhyadx\/category_7554611.html","is_audit_pending":False}]},"total":3,"un_pass_total":0,"recycle_total":0}
}
# 递归调用def read_dict(input_dict, input_key, result_list):if input_dict:if isinstance(input_dict, dict):for key1, value1 in input_dict.items():if not isinstance(value1, (dict, list)):# 删减为空的时候if value1:if key1 == input_key:result_list.append(value1)else:read_dict(input_dict[key1], input_key, result_list)elif isinstance(input_dict, list):if input_dict:for i in range(len(input_dict)):if isinstance(input_dict[i], dict):for key2, value2 in input_dict[i].items():if not isinstance(value2, (dict, list)):# 删减为空的时候if value2:if key2 == input_key:result_list.append(value2)else:read_dict(input_dict[key2], input_key, result_list)else:print("您的输入不合法!")def get_key_value(source_dict, key, index=0):out_list = []read_dict(source_dict, key, out_list)if out_list and index >= 0:return out_list[index]else:return out_listinput_dict_value = "qw"
input_key_value = 'id'
num_value = 2
result = get_key_value(input_dict_value, input_key_value, num_value)
if result:print(result)

2 从文件中获取指定字符所在行数据。实现函数:check_string(file_path, key_word),file_path代表文件所在路径,key_word代码搜索的关键字,返回符合条件的列表

"""
File: 03file取list.py
Author: chde_wang
Date: 2020-08-01 14:39:59
Description:从文件中获取指定字符所在行数据。
"""def check_string(file_path, key_word):""":param file_path: 代表文件所在路径:param key_word: 代码搜索的关键字:return: 返回符合条件的列表"""result_list = []try:with open(file_path, 'r') as f:for line in f.readlines():line = line.strip('\n')if line.find(key_word) != -1:result_list.append(line)except FileNotFoundError:print("文件不存在或者文件打开错误!")return result_listfind_s = "complate"
result_lst = []
result_lst = check_string("test.txt", find_s)
if not result_lst:print("您查找结果为空")
else:print("您查找结果为:", result_lst)

python习题week3相关推荐

  1. 【Python习题】计算弓形的面积(保姆级图文+实现代码)

    目录 题目 实现思路 实现代码 总结 主要内容是校设课程的习题和课外学习的一些习题. 欢迎关注 『Python习题』 系列,持续更新中 欢迎关注 『Python习题』 系列,持续更新中 题目 题目 如 ...

  2. 百分制成绩转换五分制F【Python习题】(保姆级图文+实现代码)

    目录 题目 描述 输入格式 输出格式 输入输出示例 思路 代码 实现效果 总结 主要内容是校设课程的习题和课外学习的一些习题. 欢迎关注 『Python习题』 系列,持续更新中 欢迎关注 『Pytho ...

  3. 【Python习题】简易英汉字典(project-ssss)(题目的坑解析+实现代码)

    目录 题目 示例 1‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬ ...

  4. 浙大python习题超详细思路(第二章)

    人生苦短,我用python https://pintia.cn/problem-sets/1111652100718116864/problems/type/7 题源来自pta 没有读者验证码,只是验 ...

  5. Python 习题 老虎、棒棒、鸡、虫

    Python 习题 老虎.棒棒.鸡.虫 看了好多人写的同项目代码,好长,想着写个短的. import time,random def pd(n,t):if (n == 0 and t == 4) or ...

  6. Python.习题五 列表与元组(下)

    Python.<习题五> 列表与元组 11.假设列表lst_info=[["李玉","男",25],["金忠","男& ...

  7. python高级练习题:多米诺平铺 - 5×2N局【难度:4级】--景越Python编程实例训练营,不同难度Python习题,适合自学Python的新手进阶

    python高级练习题:多米诺平铺 - 5×2N局[难度:4级]: 请还检查了在[多米诺拼接系列]其他练习题(https://www.codewars.com/collections/5d19554d ...

  8. Python习题十三套汇总

    前言: Python习题系列结束,共计13套. 查找具体题目,按Ctrl+F搜关键字,定位到相关习题,再点击标题链接进入. 本章整合13套链接,如下: Python习题 习题一 接受多个整数输出最大值 ...

  9. python进阶练习题:馏分类【难度:2级】--景越Python编程实例训练营,不同难度Python习题,适合自学Python的新手进阶

    python进阶练习题:馏分类[难度:2级]: 为您提供了类"分数",这两个参数(分子,分母)的骨架. 例: fraction1 =分数(4,5) 分数fraction1 =新分数 ...

最新文章

  1. android 获取手机GSM/CDMA信号信息,并获得基站信息
  2. linux user 用户管理
  3. 黑马程序员--网络编程
  4. linux监测指定进程的CPU及物理内存消耗情况(c程序)
  5. springboot微服务 java b2b2c电子商务系统(一)服务的注册与发现(Eureka)
  6. vscode php插件_vscode+phpstudy+xdebug无法断点(踩坑记)
  7. eclipse class 中无法打断点
  8. Keras中RNN、LSTM、GRU等输入形状batch_input_shape=(batch_size,time_steps,input_dim)及TimeseriesGenerator详解
  9. vue项目之不一样的axios封装(+防抖函数)
  10. 技术对接场景,打破创新窘境
  11. XTU oj 1112 三点共线
  12. processing作业:画有五角星绕圈的国旗(此例:塔吉克斯坦共和国国旗)
  13. 无线路由器在信号无法连接网络连接服务器,能搜索到wifi信号连接不上是怎么回事?...
  14. Nvidia TX2 刷机全过程
  15. python爱心代码合集
  16. Android NDK编译libjpeg-turbo压缩图片
  17. 自然资源“数字赋能”重点任务进行梳理
  18. Python实现淘宝秒杀
  19. 使用simsun的黑体
  20. 微信原创文章写作平台有哪些

热门文章

  1. php 域名白名单,域名白名单验证
  2. oracle数据库日期加一,日期和Oracle数据库
  3. 网络中的计算机如果加入家庭组,win10系统加入其他计算机家庭组的操作方法
  4. 关于引入 js 文件
  5. MySQL MVCC 概述
  6. jenkins使用哪个版本号_Linux下安装JDK及jenkins
  7. Spring 全局异常捕获
  8. 教你如何一篇博客读懂设计模式之—--工厂模式
  9. 深度残差网络_注意力机制+软阈值化=深度残差收缩网络
  10. (找规律)Magic of David Copperfield