首页 文章 精选 留言 我的

精选列表

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

python标准库学习5 ---bisect — Array bisection algorithm

#coding=utf-8 import bisect list = [ 1 , 2 , 3 , 4 , 6 , 7 , 8 , 9 ] #假定list已经排序 print bisect.bisect_left( list , 5 ) #返回5应该插入的索引位置 print bisect.bisect_right( list , 5 ) print bisect.bisect( list , 5 ) bisect.insort_left( list , 5 , 0 , len ( list )) print list bisect.insort_right( list , 5 ) print list def index(a, x): 'Locate the leftmost value exactly equal to x' i = bisect_left(a, x) if i ! = len (a) and a[i] = = x: return i raise ValueError def find_lt(a, x): 'Find rightmost value less than x' i = bisect_left(a, x) if i: return a[i - 1 ] raise ValueError def find_le(a, x): 'Find rightmost value less than or equal to x' i = bisect_right(a, x) if i: return a[i - 1 ] raise ValueError def find_gt(a, x): 'Find leftmost value greater than x' i = bisect_right(a, x) if i ! = len (a): return a[i] raise ValueError def find_ge(a, x): 'Find leftmost item greater than or equal to x' i = bisect_left(a, x) if i ! = len (a): return a[i] raise ValueError >>> def grade(score, breakpoints = [ 60 , 70 , 80 , 90 ], grades = 'FDCBA' ): ... i = bisect(breakpoints, score) ... return grades[i] ... >>> [grade(score) for score in [ 33 , 99 , 77 , 70 , 89 , 90 , 100 ]] [ 'F' , 'A' , 'C' , 'C' , 'B' , 'A' , 'A' ] >>> data = [( 'red' , 5 ), ( 'blue' , 1 ), ( 'yellow' , 8 ), ( 'black' , 0 )] >>> data.sort(key = lambda r: r[ 1 ]) >>> keys = [r[ 1 ] for r in data] # precomputed list of keys >>> data[bisect_left(keys, 0 )] ( 'black' , 0 ) >>> data[bisect_left(keys, 1 )] ( 'blue' , 1 ) >>> data[bisect_left(keys, 5 )] ( 'red' , 5 ) >>> data[bisect_left(keys, 8 )] ( 'yellow' , 8 ) ============================================================================== 本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2011/11/26/2264244.html,如需转载请自行联系原作者

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

Python-利用beautifulsoup写个豆瓣热门图书爬虫

Anaconda3里边自带了bs4的包,省的我自己安装了。 最近觉得模块化的写法可以让代码变得清晰易读。而且随着代码的增多,找bug也会更方便。(目前我还写不出这么多)而且模块化有种工具化的思想,拿来主义的思想在里面,使用工具可是人等少数智慧动物的专利啊。之后也要多学习使用[try - except]的写法,可以直观的看出错误。 初学网页爬虫,目前只会爬取豆瓣这样清晰好看的静态网页,对于复杂的js控制的动态网页,我现在还束手无策。 1 # -*- coding: utf-8 -*- 2 """ 3 Created on Tue Jan 2 17:44:30 2018 4 5 @author: xglc 6 找到豆瓣图书的【新书速递】内容 7 """ 8 import requests 9 from bs4 import BeautifulSoup 10 11 def _gethtml(): 12 try: 13 req = requests.get('https://book.douban.com/') 14 data1 = [] 15 data1.append(req.text) 16 except Exception as e: 17 raise e 18 return data1 19 20 def _getdata(html): 21 title = [] 22 author = [] 23 data2 = {} 24 soup = BeautifulSoup(html,'html.parser') 25 for li in soup.find('ul',attrs={'class':'list-col list-col5 list-express slide-item'}).find_all("li"): 26 title.append(li.find('div',class_='info').find('div',class_='title').text) 27 author.append(li.find('div',class_='info').find('div',class_='author').text) 28 data2['title'] = title 29 data2['author'] = author 30 # print (data2) 31 return data2 32 33 def _txt(data3): 34 with open('f://book.txt','w') as f: 35 for title in data['title']: 36 f.write(title) 37 f.close 38 39 if __name__ == '__main__': 40 htmls = _gethtml() 41 data = _getdata(htmls[0]) 42 _txt(data) 43 # print (data['title']) View Code

资源下载

更多资源
Nacos

Nacos

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

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

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

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册