首页 文章 精选 留言 我的

精选列表

搜索[基础搭建],共10000篇文章
优秀的个人博客,低调大师

Spring Boot基础教程10-web应用开发-Servlets, Filters, listeners

Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、 Filter、Listener等等 二.在spring boot中的三种实现方式 方法一:通过注册ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制 /** * 自定义servlet * * @author wujing */ public class CustomServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("servlet get method"); doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("servlet post method"); response.getWriter().write("hello world"); } } /** * 自定义filter * * @author wujing */ public class CustomFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init filter"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("do filter"); chain.doFilter(request, response); } @Override public void destroy() { System.out.println("destroy filter"); } } /** * 自定义listener * * @author wujing */ public class CustomListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("contextInitialized"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("contextDestroyed"); } } 注册bean @Bean public ServletRegistrationBean servletRegistrationBean() { return new ServletRegistrationBean(new CustomServlet(), "/roncoo"); } @Bean public FilterRegistrationBean filterRegistrationBean() { return new FilterRegistrationBean(new CustomFilter(), servletRegistrationBean()); } @Bean public ServletListenerRegistrationBean<CustomListener> servletListenerRegistrationBean() { return new ServletListenerRegistrationBean<CustomListener>(new CustomListener()); } 方法二:通过实现 ServletContextInitializer 接口直接注册 implements ServletContextInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.addServlet("customServlet", new CustomServlet()).addMapping("/roncoo"); servletContext.addFilter("customFilter", new CustomFilter()) .addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "customServlet"); servletContext.addListener(new CustomListener()); } 方法三:在 SpringBootApplication 上使用@ServletComponentScan 注解后,直接通过@WebServlet、@WebFilter、@WebListener 注解自动注册

优秀的个人博客,低调大师

Spring Boot基础教程9-web应用开发-错误处理

一.错误的处理 方法一:Spring Boot 将所有的错误默认映射到/error, 实现ErrorController @Controller @RequestMapping(value = "error") public class BaseErrorController implements ErrorController { private static final Logger logger = LoggerFactory.getLogger(BaseErrorController.class); @Override public String getErrorPath() { logger.info("出错啦!进入自定义错误控制器"); return "error/error"; } @RequestMapping public String error() { return getErrorPath(); } } 方法二:添加自定义的错误页面 2.1 html静态页面:在resources/public/error/ 下定义 如添加404页面: resources/public/error/404.html页面,中文注意页面编码 2.2 模板引擎页面:在templates/error/下定义 如添加5xx页面: templates/error/5xx.ftl 注:templates/error/ 这个的优先级比较 resources/public/error/高 方法三:使用注解@ControllerAdvice /** * 统一异常处理 * * @param exception * exception * @return */ @ExceptionHandler({ RuntimeException.class }) @ResponseStatus(HttpStatus.OK) public ModelAndView processException(RuntimeException exception) { logger.info("自定义异常处理-RuntimeException"); ModelAndView m = new ModelAndView(); m.addObject("roncooException", exception.getMessage()); m.setViewName("error/500"); return m; } /** * 统一异常处理 * * @param exception * exception * @return */ @ExceptionHandler({ Exception.class }) @ResponseStatus(HttpStatus.OK) public ModelAndView processException(Exception exception) { logger.info("自定义异常处理-Exception"); ModelAndView m = new ModelAndView(); m.addObject("roncooException", exception.getMessage()); m.setViewName("error/500"); return m; }

优秀的个人博客,低调大师

[雪峰磁针石博客]python网络基础工具书籍下载-持续更新

爬虫书籍参见: 2018最佳人工智能数据采集(爬虫)工具书下载 Python Network Programming Cookbook, 2nd Edition - 2017.pdf 介绍了现实世界中几乎所有网络任务的真实示例,通过简明易懂的形式让读者掌握如何使用Python完成这些网络编程任务。具体说来,书中通过70多篇攻略讨论了Python网络编程的高阶话题,包括编写简单的网络客户端和服务器、HTTP协议网络编程、跨设备编程、屏幕抓取以及网络安全监控,等等。本书可以作为任何一门网络编程课程中培养实践技能的补充材料。《图灵程序设计丛书:Python网络编程攻略》需要读者对Python语言及TCP/IP等基本的网络概念有了解。 Practical Network Automation Leverage the power of Python and Ansible to optimize your network - 2017.pdf O'Reilly.Twisted.Network.Programming.Essentials.2nd.Edition.Mar.2013.pdf Learning Python Network Programming - 2015.pdf Foundations of Python Network Programming, 3rd Edition - 2014.pdf Data Science and Complex Networks Real Case Studies with Python - 2016.pdf 参考资料 讨论qq群144081101 591302926 567351477 钉钉免费群21745728 本文最新版本地址 本文涉及的python测试开发库 谢谢点赞! 本文相关海量书籍下载 Complex Network Analysis in Python Recognize – Construct – Visualize – Analyze – Interpret - 2018.pdf

优秀的个人博客,低调大师

02.Beetl模板的基础用法 【变量、循环、条件】---《Beetl视频课程》

本期视频做了一个博客的首页列表; 内容简介:springboot 集成 beetlsql;使用for循环,使用if控制语句,使用虚拟属性,定义变量等等 一起学beetl目录:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598 作者:GK 集成BeetlSql,用来查询数据库 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.5</version> </dependency> application.properties中增加数据库配置 spring.datasource.url=jdbc:mysql://mysql:3306/blog?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false&useInformationSchema=true spring.datasource.username=root spring.datasource.password=8975789757 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 增加数据源配置类 package com.ibeetl.blog.config; import com.zaxxer.hikari.HikariDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import javax.sql.DataSource; /** * @author GavinKing * @ClassName: DBConfig * @Description: * @date 2018/11/18 */ @Configuration public class DBConfig { @Bean(name = "datasource") public DataSource datasource(Environment env) { HikariDataSource ds = new HikariDataSource(); ds.setJdbcUrl(env.getProperty("spring.datasource.url")); ds.setUsername(env.getProperty("spring.datasource.username")); ds.setPassword(env.getProperty("spring.datasource.password")); ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name")); return ds; } } 修改模板文件为html结尾 配置模板文件为html 结尾,在application.properties中增加配置 beetl.suffix=html 变量 临时变量:使用var 定义;类似js语法;全局变量:整个模板都能访问的变量;通过 template.bind("key",object)去定义共享变量:所有的模板都能访问的变量;通过 GroupTemplate gt = new GroupTemplate(resourceLoader, cfg); Map<String,Object> shared = new HashMap<String,Object>(); shared.put("name", "beetl"); gt.setSharedVars(shared); 去定义。模板变量:相当于用一个变量表示一段模板或者代码; <% var content = { var c = "1234"; print(c); %> 模板其他内容: <% }; %> 循环语句 for循环,支持 for-in和for(exp,exp,exp) for(blog in page.list){ .... } for(var i=0;i<page.list.~size){ page.list[i].... } 其他循环语句 var i = 0; while(i<5){ print(i); i++; } //--------elsefor用法,如果for没有进行循环时执行---------- var list = []; for(item in list){ }elsefor{ print("未有记录"); } 条件语句 var a =true; var b = 1; if(a&&b==1){ }else if(a){ }else{ } //---------switch------- var b = 1; switch(b){ case 0: print("it's 0"); break; case 1: print("it's 1"); break; default: print("error"); } //---------select,更加强大的switch------- var b = 1; select(b){ case 0,1: print("it's small int"); case 2,3: print("it's big int"); default: print("error"); } //----------- var b = 1; select{ case b<1,b>10: print("it's out of range"); break; case b==1: print("it's 1"); break; default: print("error"); } 时间格式化 ${date,"yyyy-MM-dd"} 这样就能格式化时间了,嗯,超简单 项目git地址:https://gitee.com/gavink/beetl-blog 视频地址:下载下来会更清晰,说话比较慢,建议 1.2x倍速播放 百度网盘下载: https://pan.baidu.com/s/1LyxAxlKpVXgVjwSXIbzBuA 提取码: 68im bilibili (可以调节清晰度): https://www.bilibili.com/video/av36278644/?p=2 博客目录:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598

优秀的个人博客,低调大师

基础兴趣或者转行学习Python,我们应该如何入门呢?

Python 语言应该如何入门,记得我几年前也碰到过这样的问题,当时网上随便搜了一下饥不择食的找了一些书开始啃起来,结果发现很疑惑,感觉吃力,走了很多弯路。若不得法还会降低初学者的兴趣,现在我就说说自己对python 入门的理解. 学Python和学其他的语言其实是相同的,我给新同事讲课的时候就说学编程和练武功其实是很相似,入门大致这样几步: 找本靠谱的书, 找个靠谱的师傅, 找一个地方开始练习。 学语言也是的:选一本通俗易懂的书,找一个好的视频资料,然后自己装一个IDE工具开始边学变写。下面我具体来讲讲: 1.找一本靠谱的书,难度一定要是入门级别,千万不能太复杂,不要一下子陷进去,会打乱节奏,学东西要循序渐进,不能一口吃个胖子.打个比方,学过java的同学都听过大名鼎鼎的thinking in java,这边书很厚很全,若一上来就学,肯定会吃力,时间长了就会失去兴趣,因此对初学者来说,一定要找一个通熟易懂的,简单的书。入门的书非常关键。 入门的书很多,但是我个人强烈推荐"A Byte of Python",这本书我读了2遍,作者写的思路非常清晰,对每一个知识点讲解的很到位,不多不少,刚刚好,对初学者来说,力道刚刚好。而且是全英文,对提高自己的英语水平,很有帮助. 2.找一个好的视频资源,当然若你周围有Python高手就更好了,可以多交流多请教。视频资源我推荐imooc,当然有人说还有jikexueyuan,网易公开课,这几家我都看过一些,各有千秋,我建议初学者还是选择imooc,他家的Python讲解的很仔细,而且音频的质量比较高,最关键是的可以在web上直接编程练习,都不用自己安装编译器,非常方便。居家旅行必备啊~~ 3.多编写程序,这似乎是废话,但是确实是一句实话。学编程一定要亲身去编写,没有什么捷径.一开始哪怕你把书里面的例子一字不落敲一遍,也好过你只是去看书,而不动手。 而且学Python最好是坚持编,每天抽小半个小时,学一些知识点,不断的坚持.大概快的话几个星期基本就能入门了。 编推荐一个学python的学习qun 227 435 450,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享! 以上就是我对Python入门的感悟,希望对初学者能有一点帮助,能帮到一些人少走一点弯路.也就不枉我大半夜在这里码字了~~

优秀的个人博客,低调大师

Python基础系列-用paramiko写一个简单的ssh终端

版权声明:如需转载,请注明转载地址。 https://blog.csdn.net/oJohnny123/article/details/82144008 #!/usr/bin/python # -*- coding: UTF-8 -*- """ Created by liaoyangyang1 on 2017/11/7. """ import paramiko import sys import socket import select from paramiko.py3compat import u tran = paramiko.Transport(('ip', 22,)) tran.start_client() tran.auth_password('username', 'password') # 打开一个通道 chan = tran.open_session() # 获取一个终端 chan.get_pty() # 激活器 chan.invoke_shell() while True: # 监视用户输入和服务器返回数据 # sys.stdin 处理用户输入 # chan 是之前创建的通道,用于接收服务器返回信息 readable, writeable, error = select.select([chan, sys.stdin, ], [], [], 1) if chan in readable: try: x = u(chan.recv(1024)) if len(x) == 0: print('\r\n*** EOF\r\n') break sys.stdout.write(x) sys.stdout.flush() except socket.timeout: pass if sys.stdin in readable: inp = sys.stdin.readline() chan.sendall(inp) chan.close() tran.close()

优秀的个人博客,低调大师

多线程并发相关的几个重要基础知识点解析

问:volatile 变量和 atomic 变量有什么不同? 答:volatile 变量和 atomic 变量看起来很像,但功能却不一样。 volatile 变量可以确保先行关系,即写操作会发生在后续的读操作之前, 但它并不能保证原子性,也就是说其可以保证变量的可见性与有序性,无法保证原子性。例如用 volatile 修饰 count 变量,则 count++ 操作就不是原子性的。 AtomicInteger 类提供的 atomic 方法可以让这种操作具有原子性,其保证了并发安全的可见性、有序性、原子性三要素。如 getAndIncrement() 方法会原子性的进行增量操作把当前值加一,其它数据类型和引用变量也可以进行相似操作。 问:如果你提交任务时线程池队列已满了则会发会生什么? 答:这取决于线程池队列的实现。 如果线程池使用的是 LinkedBlockingQueue 无界队列,则继续添加任务到阻塞队列中等待执行理论上是不会满的,因为 LinkedBlockingQueue 可以近乎认为是一个无穷大的队列,可以无限存放任务。 如果线程池使用的是有界队列(比如 ArrayBlockingQueue)的话,任务首先会被添加到队列中,当队列满了则会使用拒绝策略 RejectedExecutionHandler 处理满了的任务,默认是 AbortPolicy,我们可以在创建线程池时修改这个策略,譬如是静默忽略还是崩溃等。 问:简单说说并发饥饿与死锁的区别? 答:饥饿是指系统不能保证某个进程的等待时间上界,从而使该进程长时间等待,当等待时间给进程推进和响应带来明显影响时,称发生了进程饥饿。当饥饿到一定程度的进程所赋予的任务即使完成也不再具有实际意义时称该进程被饿死。 死锁是指在多道程序系统中,一组进程中的每一个进程都无限期等待被该组进程中的另一个进程所占有且永远不会释放的资源。 他们的相同点是二者都由于竞争资源而引起。 他们的不同点可以从几个方面来总结: 从进程状态考虑,死锁进程都处于等待状态,忙等待(处于运行或就绪状态)的进程并非处于等待状态,但却可能被饿死; 死锁进程等待永远不会被释放的资源,饿死进程等待会被释放但却不会分配给自己的资源,表现为等待时限没有上界(排队等待或忙式等待); 死锁一定发生了循环等待,而饿死则不然。这也表明通过资源分配图可以检测死锁存在与否,但却不能检测是否有进程饿死; 死锁一定涉及多个进程,而饥饿或被饿死的进程可能只有一个; 在饥饿的情形下,系统中有至少一个进程能正常运行,只是饥饿进程得不到执行机会,而死锁则可能会最终使整个系统陷入死锁并崩溃; 欢迎工作一到五年的Java工程师朋友们加入Java架构开发:744677563 本群提供免费的学习指导 架构资料 以及免费的解答 不懂得问题都可以在本群提出来 之后还会有职业生涯规划以及面试指导

优秀的个人博客,低调大师

Python3 与 NetCore 基础语法对比(List、Tuple、Dict、Set专栏)

Jupyter最新版:https://www.cnblogs.com/dotnetcrazy/p/9155310.html 在线演示:http://nbviewer.jupyter.org/github/lotapp/BaseCode/blob/master/python/notebook/1.POP/3.list_tuple_dict 更新:新增Python可变Tuple、List切片、Set的扩展:https://www.cnblogs.com/dotnetcrazy/p/9155310.html#extend 今天说说List和Tuple以及Dict。POP部分还有一些如Func、IO(也可以放OOP部分说)然后就说说面向对象吧。 先吐槽一下:Python面向对象真心需要规范,不然太容易走火入魔了 -_-!!! 汗,下次再说。。。 对比写作真的比单写累很多,希望大家多捧捧场 ^_^ 进入扩展:https://www.cnblogs.com/dotnetcrazy/p/9155310.html#ext 步入正题: 1.列表相关: Python定义一个列表(列表虽然可以存不同类型,一般我们把相同类型的值存列表里面,不同类型存字典里(key,value)) info_list=[] #空列表 infos_list=["C#","JavaScript"] 遍历和之前一样,for 或者 while 都可以(for扩展:https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse) NetCore:var infos_list = new List<object>() { "C#", "JavaScript" }; 遍历可以用foreach,for,while Python列表的添加: # 末尾追加infos_list. append("Java") # 添加一个列表infos_list. extend(infos_list2) # 指定位置插入infos_list. insert(0,"Python") # 插入列表:infos_list .insert(0,temp_list) 看后面的 列表嵌套,是通过下标方式获取,eg: infos_list[0][1] Python在指定位置插入列表是真的插入一个列表进去,C#是把里面的元素挨个插入进去 NetCore:Add,AddRange,Insert,InsertRange (和Python插入列表有些区别) Python列表删除系列: infos_list. pop() #删除最后一个 infos_list. pop(0) #删除指定索引,不存在就报错 infos_list. remove("张三") # remove("")删除指定元素 ,不存在就报错 delinfos_list[1]#删除指定下标元素,不存在就报错 del infos_list#删除集合(集合再访问就不存在了)不同于C#给集合赋null 再过一遍 NetCore:移除指定索引:infos_list.RemoveAt(1); 移除指定值: infos_list.Remove(item);清空列表:infos_list.Clear(); Python修改:(只能通过索引修改) infos_list2[1]="PHP" #只有下标修改一种方式, 不存在则异常 # 想按值修改需要先查下标再修改 eg: infos_list2.index("张三") infos_list2[0]="GO" # infos_list2.index("dnt")# 不存在则异常 # 知识面拓展: https://www.zhihu.com/question/49098374 # 为什么python中不建议在for循环中修改列表? # 由于在遍历的过程中,删除了其中一个元素,导致后面的元素整体前移,导致有个元素成了漏网之鱼。 # 同样的,在遍历过程中,使用插入操作,也会导致类似的错误。这也就是问题里说的无法“跟踪”元素。 # 如果使用while,则可以在面对这样情况的时候灵活应对。 NetCore:基本上和Python一样 Python查询系列:in, not in, index, count if "张三" in names_list: names_list.remove("张三") if "大舅子" not in names_list: names_list.append("大舅子") names_list.index("王二麻子") names_list.count("逆天") NetCore:IndexOf , Count 查找用Contains,其他的先看看,后面会讲 Python排序 num_list. reverse() # 倒序 num_list. sort() # 从小到大排序 num_list. sort(reverse=True) # 从大到小 列表嵌套,获取用下标的方式:num_list[5][1] NetCore:var num_list2 = new List<object>() { 33, 44, 22,new List<object>(){11,55,77} }; 不能像python那样下标操作,可以定义多维数组来支持 num_list2[i][j] (PS,其实这个嵌套不太用,以后都是列表里面套Dict,类似与Json) 2.Tuple 元组 这次先说NetCore吧:(逆天ValueTuple用的比较多,下面案例就是用的这个) 元组系: https://msdn.microsoft.com/zh-cn/library/system.tuple.aspx 值元组: https://msdn.microsoft.com/zh-cn/library/system.valuetuple.aspx C#中元组主要是方便程序员,不用自然可以。比如:当你返回多个值是否还用ref out 或者返回一个list之类的?这些都需要先定义,比较麻烦.元祖在这些场景用的比较多。 先说说基本使用: 初始化: var test_tuple = ("萌萌哒", 1, 3, 5, "加息", "加息"); //这种方式就是valueTuple了(看vscode监视信息) 需要说下的是,取值只能通过 itemxxx来取了,然后就是 valueTuple的值是可以修改的 忽略上面说的(一般不会用的),直接进应用场景: 就说到这了,代码部分附录是有的 Python:用法基本上和列表差不多( 下标和前面说的用法一样,比如test_tuples[-1] 最后一个元素) 定义:一个元素: test_tuple1=(1,) test_tuple=("萌萌哒",1,3,5,"加息","加息") test_tuple.count("加息") test_tuple. index("萌萌哒") #没有find方法 test_tuple. index("加息", 1, 4) #从特定位置查找, 左闭右开区间==>[1,4) 来说说拆包相关的,C#的上面说了,这边来个案例即可: a=(1,2) b=a #把a的引用给b c,d=a #不是把a分别赋值给c和d, 等价于:c=a[0] d=a[1] 来个扩展吧(多维元组): some_tuples=[(2,"萌萌哒"),(4,3)] some_tuples[0] some_tuples[0][1] 3.Dict系列 Python遍历相关: #每一次相当于取一个元组,那可以用之前讲的例子来简化了:c,d=a #等价于:c=a[0] d=a[1] for k,v in infos_dict.items(): print("Key:%s,Value:%s"%(k,v)) NetCore:方式和Python差不多 foreach (KeyValuePair<string, object> kv in infos_dict) { Console.WriteLine($"Key:{kv.Key},Value:{kv.Value}"); } Python增删改系列: 增加、修改:infos_dict["wechat"]="dotnetcrazy" #有就修改,没就添加 删除系列: # 删除 del infos_dict["name"]#不存在就报错 #清空字典内容 infos_dict.clear() # 删除字典 del infos_dict NetCore: 添加:infos_dict.Add("wechat", "lll");infos_dict["wechat1"] = "lll"; 修改: infos_dict["wechat"] = "dotnetcrazy"; 删除: infos_dict.Remove("dog"); //不存在不报错 infos_dict.Clear(); //列表内容清空 Python查询系列:推荐:infos_dict.get("mmd")#查不到不会异常 NetCore:infos_dict["name"] 可以通过ContainsKey(key) 避免异常。看值就 ContainsValue(value) 扩展: 1.多维元组: some_tuples=[(2,"萌萌哒"),(4,3)] some_tuples[0] some_tuples[0][1] 2.运算符扩展:(+,*,in,not in) # 运算符扩展: test_str="www.baidu.com" test_list=[1,"d",5] test_dict={"name":"dnt","wechat":"xxx"} test_list1=[2,4,"n","t",3] # + 合并 (不支持字典) print(test_str+test_str) print(test_list+test_list1) # * 复制 (不支持字典) print(test_str*2) print(test_list*2) # in 是否存在(字典是查key) print("d" in test_str) #True print("d" in test_list) #True print("d" in test_dict) #False print("name" in test_dict) #True # not in 是否不存在(字典是查key) print("z" not in test_str) #True print("z" not in test_list) #True print("z" not in test_dict) #True print("name" not in test_dict) #False 3.内置函数扩展:(len,max,min,del) len(),这个就不说了,用的太多了 max(),求最大值,dict的最大值是比较的key 这个注意一种情况(当然了,你按照之前说的规范,list里面放同一种类型就不会出错了) min(),这个和max一样用 del() or del xxx删完就木有了 #可以先忽略cmp(item1, item2)比较两个值 #是Python2里面有的 cmp(1,2) ==> -1 #cmp在比较字典数据时,先比较键,再比较值 知识扩展: 可变的元组(元组在定义的时候就不能变了,但是可以通过类似这种方式来改变) List切片: Set集合扩展: 更新:(漏了一个删除的方法): 概念再补充下: # dict内部存放的顺序和key放入的顺序是没有关系的# dict的key必须是不可变对象(dict根据key进行hash算法,来计算value的存储位置# 如果每次计算相同的key得出的结果不同,那dict内部就完全混乱了) 用一张图理解一下:(测试结果:元组是可以作为Key的 -_-!) 附录Code: Python:https://github.com/lotapp/BaseCode/tree/master/python/1.POP/3.list_tuple_dict Python List: # 定义一个列表,列表虽然可以存不同类型,一般我们把相同类型的值存列表里面,不同类型存字典里(key,value) infos_list=["C#","JavaScript"]#[] # ########################################################### # # 遍历 for while # for item in infos_list: # print(item) # i=0 # while i<len(infos_list): # print(infos_list[i]) # i+=1 # ########################################################### # # 增加 # # 末尾追加 # infos_list.append("Java") # print(infos_list) # # 指定位置插入 # infos_list.insert(0,"Python") # print(infos_list) # temp_list=["test1","test2"] # infos_list.insert(0,temp_list) # print(infos_list) # # 添加一个列表 # infos_list2=["张三",21]#python里面的列表类似于List<object> # infos_list.extend(infos_list2) # print(infos_list) # # help(infos_list.extend)#可以查看etend方法描述 # ########################################################### # # 删除 # # pop()删除最后一个元素,返回删掉的元素 # # pop(index) 删除指定下标元素 # print(infos_list.pop()) # print(infos_list) # print(infos_list.pop(0)) # # print(infos_list.pop(10)) #不存在就报错 # print(infos_list) # # remove("")删除指定元素 # infos_list.remove("张三") # # infos_list.remove("dnt") #不存在就报错 # print(infos_list) # # del xxx[index] 删除指定下标元素 # del infos_list[1] # print(infos_list) # # del infos_list[10] #不存在就报错 # # del infos_list #删除集合(集合再访问就不存在了) # ########################################################### # # 修改 xxx[index]=xx # # 注意:一般不推荐在for循环里面修改 # print(infos_list2) # infos_list2[1]="PHP" #只有下标修改一种方式 # # infos_list2[3]="GO" #不存在则异常 # print(infos_list2) # # 想按值修改需要先查下标再修改 # infos_list2.index("张三") # infos_list2[0]="GO" # print(infos_list2) # # infos_list2.index("dnt")#不存在则异常 # # 知识面拓展: https://www.zhihu.com/question/49098374 # # 为什么python中不建议在for循环中修改列表? # # 由于在遍历的过程中,删除了其中一个元素,导致后面的元素整体前移,导致有个元素成了漏网之鱼。 # # 同样的,在遍历过程中,使用插入操作,也会导致类似的错误。这也就是问题里说的无法“跟踪”元素。 # # 如果使用while,则可以在面对这样情况的时候灵活应对。 ########################################################### # # 查询 in, not in, index, count # # # for扩展:https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse # names_list=["张三","李四","王二麻子"] # # #张三在列表中执行操作 # if "张三" in names_list: # names_list.remove("张三") # print(names_list) # # #查看"大舅子"不在列表中执行操作 # if "大舅子" not in names_list: # names_list.append("大舅子") # print(names_list) # # #查询王二麻子的索引 # print(names_list.index("王二麻子")) # print(names_list.count("大舅子")) # print(names_list.count("逆天")) ########################################################### # # 排序(sort, reverse 逆置) # num_list=[1,3,5,88,7] # #倒序 # num_list.reverse() # print(num_list) # # 从小到大排序 # num_list.sort() # print(num_list) # # 从大到小 # num_list.sort(reverse=True) # print(num_list) # # ########################################################### # # #列表嵌套(列表也是可以嵌套的) # num_list2=[33,44,22] # num_list.append(num_list2) # print(num_list) # # for item in num_list: # # print(item,end="") # print(num_list[5]) # print(num_list[5][1]) # # ########################################################### # # # 引入Null==>None # # a=[1,2,3,4] # # b=[5,6] # # a=a.append(b)#a.append(b)没有返回值 # # print(a)#None View Code Python Tuple: # 只能查询,其他操作和列表差不多(不可变) test_tuple=("萌萌哒",1,3,5,"加息","加息") # count index print(test_tuple.count("加息")) print(test_tuple.index("萌萌哒"))#没有find方法 # 注意是左闭右开区间==>[1,4) # print(test_tuple.index("加息", 1, 4))#查不到报错:ValueError: tuple.index(x): x not in tuple #下标取 print(test_tuple[0]) # 遍历 for item in test_tuple: print(item) i=0 while i<len(test_tuple): print(test_tuple[i]) i+=1 # 扩展: test_tuple1=(1,) #(1)就不是元祖了 test_tuple2=(2) print(type(test_tuple1)) print(type(test_tuple2)) # # ============================================== # 扩展:(后面讲字典遍历的时候会再提一下的) a=(1,2) b=a#把a的引用给b #a里面两个值,直接给左边两个变量赋值了(有点像拆包了) c,d=a #不是把a分别赋值给c和d,等价于:c=a[0] d=a[1] print(a) print(b) print(c) print(d) View Code Python Dict: infos_dict={"name":"dnt","web":"dkill.net"} # # 遍历 # for item in infos_dict.keys(): # print(item) # #注意,如果你直接对infos遍历,其实只是遍历keys # for item in infos_dict: # print(item) # for item in infos_dict.values(): # print(item) # for item in infos_dict.items(): # print("Key:%s,Value:%s"%(item[0],item[1])) # #每一次相当于取一个元组,那可以用之前讲的例子来简化了:c,d=a #等价于:c=a[0] d=a[1] # for k,v in infos_dict.items(): # print("Key:%s,Value:%s"%(k,v)) # # 增加 修改 (有就修改,没就添加) # # 添加 # infos_dict["wechat"]="lll" # print(infos_dict) # # 修改 # infos_dict["wechat"]="dotnetcrazy" # print(infos_dict) # # 删除 # del infos_dict["name"] # del infos_dict["dog"] #不存在就报错 # print(infos_dict) # #清空字典内容 # infos_dict.clear() # print(infos_dict) # # 删除字典 # del infos_dict # 查询 infos_dict["name"] # infos_dict["mmd"] #查不到就异常 infos_dict.get("name") infos_dict.get("mmd")#查不到不会异常 # 查看帮助 # help(infos_dict) len(infos_dict) #有几对key,value # infos_dict.has_key("name") #这个是python2里面的 View Code NetCore:https://github.com/lotapp/BaseCode/tree/master/netcore/1_POP NetCore List: // using System; // using System.Collections.Generic; // using System.Linq; // namespace aibaseConsole // { // public static class Program // { // private static void Main() // { // #region List // //# 定义一个列表 // // # infos_list=["C#","JavaScript"]#[] // var infos_list = new List<object>() { "C#", "JavaScript" }; // // var infos_list2 = new List<object>() { "张三", 21 }; // // // # ########################################################### // // // # # 遍历 for while // // // # for item in infos_list: // // // # print(item) // // foreach (var item in infos_list) // // { // // System.Console.WriteLine(item); // // } // // for (int i = 0; i < infos_list.Count; i++) // // { // // System.Console.WriteLine(infos_list[i]); // // } // // // # i=0 // // // # while i<len(infos_list): // // // # print(infos_list[i]) // // // # i+=1 // // int j=0; // // while(j<infos_list.Count){ // // Console.WriteLine(infos_list[j++]); // // } // // // # ########################################################### // // // # # 增加 // // // # # 末尾追加 // // // # infos_list.append("Java") // // // # print(infos_list) // // DivPrintList(infos_list); // // infos_list.Add("Java"); // // DivPrintList(infos_list); // // // # # 指定位置插入 // // // # infos_list.insert(0,"Python") // // // # print(infos_list) // // infos_list.Insert(0,"Python"); // // DivPrintList(infos_list); // // // # # 添加一个列表 // // // # infos_list2=["张三",21]#python里面的列表类似于List<object> // // // # infos_list.extend(infos_list2) // // // # print(infos_list) // // infos_list.AddRange(infos_list2); // // DivPrintList(infos_list); // // /*C#有insertRange方法 */ // // DivPrintList(infos_list2,"List2原来的列表:"); // // infos_list2.InsertRange(0,infos_list); // // DivPrintList(infos_list2,"List2变化后列表:"); // // // # # help(infos_list.extend)#可以查看etend方法描述 // // // # ########################################################### // // // # # 删除 // // // # # pop()删除最后一个元素,返回删掉的元素 // // // # # pop(index) 删除指定下标元素 // // // # print(infos_list.pop()) // // // # print(infos_list) // // // # print(infos_list.pop(1)) // // // # # print(infos_list.pop(10)) #不存在就报错 // // // # print(infos_list) // // // # # remove("")删除指定元素 // // // # infos_list.remove("张三") // // // # # infos_list.remove("dnt") #不存在就报错 // // // # print(infos_list) // // // # # del xxx[index] 删除指定下标元素 // // // # del infos_list[1] // // // # print(infos_list) // // // # # del infos_list[10] #不存在就报错 // // // # del infos_list #删除集合(集合再访问就不存在了) // // DivPrintList(infos_list); // // infos_list.RemoveAt(1); // // // infos_list.RemoveAt(10);//不存在则报错 // // // infos_list.RemoveRange(0,1); //可以移除多个 // // DivPrintList(infos_list); // // infos_list.Remove("我家在东北吗?"); //移除指定item,不存在不会报错 // // DivPrintList(infos_list,"清空前:"); // // infos_list.Clear();//清空列表 // // DivPrintList(infos_list,"清空后:"); // // // # ########################################################### // // // # # 修改 xxx[index]=xx // // // # # 注意:一般不推荐在for循环里面修改 // // // # print(infos_list2) // // // # infos_list2[1]="PHP" #只有下标修改一种方式 // // // # # infos_list2[3]="GO" #不存在则异常 // // // # print(infos_list2) // // DivPrintList(infos_list2); // // infos_list2[1] = "PHP"; // // // infos_list2[3]="GO"; //不存在则异常 // // DivPrintList(infos_list2); // // // # # 想按值修改需要先查下标再修改 // // // # infos_list2.index("张三") // // // # infos_list2[0]="GO" // // // # print(infos_list2) // // // # # infos_list2.index("dnt")#不存在则异常 // // int index = infos_list2.IndexOf("张三"); // // infos_list2[index] = "GO"; // // DivPrintList(infos_list2); // // infos_list2.IndexOf("dnt");//不存在返回-1 // // // ########################################################### // // // # 查询 in, not in, index, count // // // # # for扩展:https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse // // // # names_list=["张三","李四","王二麻子"] // // var names_list=new List<string>(){"张三","李四","王二麻子"}; // // // Console.WriteLine(names_list.Find(i=>i=="张三")); // // // Console.WriteLine(names_list.FirstOrDefault(i=>i=="张三")); // // Console.WriteLine(names_list.Exists(i=>i=="张三")); // // System.Console.WriteLine(names_list.Contains("张三")); // // // # #张三在列表中执行操作 // // // # if "张三" in names_list: // // // # names_list.remove("张三") // // // # else: // // // # print(names_list) // // // # #查看"大舅子"不在列表中执行操作 // // // # if "大舅子" not in names_list: // // // # names_list.append("大舅子") // // // # else: // // // # print(names_list) // // // # #查询王二麻子的索引 // // // # print(names_list.index("王二麻子")) // // // names_list.IndexOf("王二麻子"); // // // # print(names_list.count("大舅子")) // // // # print(names_list.count("逆天")) // // // Console.WriteLine(names_list.Count); // // // ########################################################### // // // # # 排序(sort, reverse 逆置) // // // # num_list=[1,3,5,88,7] // // var num_list = new List<object>() { 1, 3, 5, 88, 7 }; // // // # #倒序 // // // # num_list.reverse() // // // # print(num_list) // // num_list.Reverse(); // // DivPrintList(num_list); // // // # # 从小到大排序 // // // # num_list.sort() // // // # print(num_list) // // num_list.Sort(); // // DivPrintList(num_list); // // // # # 从大到小 // // // # num_list.sort(reverse=True) // // // # print(num_list) // // num_list.Sort(); // // num_list.Reverse(); // // DivPrintList(num_list); // // // # ########################################################### // // // # #列表嵌套(列表也是可以嵌套的) // // // # num_list2=[33,44,22] // // // # num_list.append(num_list2) // // // # print(num_list) // // var num_list2 = new List<object>() { 33, 44, 22,new List<object>(){11,55,77} }; // // DivPrintList(num_list2);//可以定义多维数组来支持 num_list2[i][j] // // // # for item in num_list: // // // # print(item) // // // # ########################################################### // // // # # 引入Null==>None // // // # a=[1,2,3,4] // // // # b=[5,6] // // // # a=a.append(b)#a.append(b)没有返回值 // // // # print(a)#None // #endregion // // Console.Read(); // } // private static void DivPrintList(List<object> list, string say = "") // { // Console.WriteLine($"\n{say}"); // foreach (var item in list) // { // System.Console.Write($"{item} "); // } // } // } // } View Code NetCore Tuple: // using System; // namespace aibaseConsole // { // public static class Program // { // private static void Main() // { // #region Tuple // // C#中元组主要是方便程序员,不用自然可以. // // 元祖系:https://msdn.microsoft.com/zh-cn/library/system.tuple.aspx // // 值元组:https://msdn.microsoft.com/zh-cn/library/system.valuetuple.aspx // // 比如:当你返回多个值是否还用ref out 或者返回一个list之类的? // // 这些都需要先定义,比较麻烦.元祖在一些场景用的比较多 eg: // // 初始化 // // var test_tuple = ("萌萌哒", 1, 3, 5, "加息", "加息"); //这种方式就是valueTuple了 // // test_tuple.Item1 = "ddd";//可以修改值 // // test_tuple.GetType(); // // test_tuple.itemxxx //获取值只能通过itemxxx // var result = GetCityAndTel(); //支持async/await模式 // var city = result.city; // var tel = result.tel; // // 拆包方式: // var (city1, tel1) = GetCityAndTel(); // #endregion // // Console.Read(); // } // // public static (string city, string tel) GetCityAndTel() // // { // // return ("北京", "110"); // // } // // 简化写法 // public static (string city, string tel) GetCityAndTel() => ("北京", "110"); // } // } View Code NetCore Dict: using System; using System.Collections.Generic; namespace aibaseConsole { public static class Program { private static void Main() { #region Dict // infos_dict={"name":"dnt","web":"dkill.net"} // # # 遍历 // # for item in infos_dict.keys(): // # print(item) // # for item in infos_dict.values(): // # print(item) // # for item in infos_dict.items(): // # print("Key:%s,Value:%s"%(item[0],item[1])) // # #每一次相当于取一个元组,那可以用之前讲的例子来简化了:c,d=a #等价于:c=a[0] d=a[1] // # for k,v in infos_dict.items(): // # print("Key:%s,Value:%s"%(k,v)) var infos_dict = new Dictionary<string, object>{ {"name","dnt"}, {"web","dkill.net"} }; // foreach (var item in infos_dict.Keys) // { // System.Console.WriteLine(item); // } // foreach (var item in infos_dict.Values) // { // System.Console.WriteLine(item); // } // foreach (KeyValuePair<string, object> kv in infos_dict) // { // // System.Console.WriteLine("Key:%s,Value:%s",(kv.Key,kv.Value)); // System.Console.WriteLine($"Key:{kv.Key},Value:{kv.Value}"); // } // // # # 增加 修改 (有就修改,没就添加) // // # # 添加 // // # infos_dict["wechat"]="lll" // // # print(infos_dict) // infos_dict.Add("wechat", "lll"); // infos_dict["wechat1"] = "lll"; // // # # 修改 // // # infos_dict["wechat"]="dotnetcrazy" // // # print(infos_dict) // infos_dict["wechat"] = "dotnetcrazy"; // // # # 删除 // // # del infos_dict["name"] // // # del infos_dict["dog"] #不存在就报错 // // # print(infos_dict) // infos_dict.Remove("name"); // infos_dict.Remove("dog"); // // # #清空列表内容 // // # infos_dict.clear() // // # print(infos_dict) // infos_dict.Clear(); // // # # 删除列表 // // # del infos_dict // # 查询 // infos_dict["name"] // infos_dict["mmd"] #查不到就异常 // infos_dict.get("name") // infos_dict.get("mmd")#查不到不会异常 Console.WriteLine(infos_dict["name"]); // Console.WriteLine(infos_dict["mmd"]); //#查不到就异常 // 先看看有没有 ContainsKey(key),看值就 ContainsValue(value) if (infos_dict.ContainsKey("mmd")) Console.WriteLine(infos_dict["mmd"]); // # 查看帮助 // help(infos_dict) // len(infos_dict) #有几对key,value Console.WriteLine(infos_dict.Count); #endregion // Console.Read(); } } } View Code 作者: 毒逆天 出处: https://www.cnblogs.com/dotnetcrazy 打赏: 18i4JpL6g54yAPAefdtgqwRrZ43YJwAV5z 本文版权归作者和博客园共有。欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接!

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册