[Python] 00 - Books
A.I. & Optimization
Advanced Machine Learning, Data Mining, and Online Advertising Services
Ref: Top 25 Python Programming Books
Ref: 廖雪峰的python教程
Ref: https://repl.it/languages/python3【在线编译调试python】
一、工程夯实策略
基础

进阶

高性能

二、自我评估
Ref: How to Hire Python Developers and Identify True Masters
/* implement */
面试问题收集
None类型
(1) 空类型的“否“ 即是None
空列表,空元组,空字典前面加not 都是None。
(2) None是全局的一个特殊的类型。
>>> type(None)
<class 'NoneType'> >>> a=None
>>> b=None
>>> id(a)==id(b)
True
is 对比 ==
== None时,调用了eq。
>>> class test():
... def __eq__(self,other):
... return True
... >>> t = test()
>>> t is None
False
>>> t == None
True
陷阱:空List参数
默认参数很有用,但使用不当,也会掉坑里。默认参数有个最大的坑,演示如下:
- 当你使用默认参数调用时,一开始结果也是对的;
- 但是,再次调用
add_end()时,结果就不对了。
def add_end(L=[]):
print('L = ' L)
L.append('END')
return L add_end()
add_end()
Output:
>>> add_end()
L = []
['END']
>>> add_end()
L = ['END']
['END', 'END']
原因解释如下:
Python函数在定义的时候,默认参数L的值就被计算出来了,即[]。
因为默认参数L也是一个变量,它指向对象[]。每次调用该函数,如果改变了L的内容,则下次调用时,默认参数的内容就变了,不再是函数定义时的[]了。
简单的说就是,函数定义后,默认参数类似于一个static变量。
陷阱:Closure的内部变量
def count():
fs = []
for i in range(1, 4):
def f():
return i*i # 问题所在:返回的该函数的i,却不属于自己
fs.append(f) return fs
f1, f2, f3 = count() print(f1())
print(f2())
print(f3()) -------
Output:
-------
9
9
9
解决方案:如果一定要引用循环变量怎么办?方法是再创建一个函数,用该函数的参数绑定循环变量当前的值,无论该循环变量后续如何更改,已绑定到函数参数的值不变:
def count():
def f(j): # 因为经过了函数的封装,所以保持了函数内部变量的独立性
def g():
return j*j
return g
fs = []
for i in range(1, 4):
fs.append(f(i)) # f(i)立刻被执行,因此i的当前值被传入f()
return fs
f1, f2, f3 = count()
偏函数
简单总结functools.partial的作用就是,把一个函数的某些参数给固定住(也就是设置默认值),返回一个新的函数,调用这个新函数会更简单。
固定子函数的某些参数
>>> import functools
>>> int2 = functools.partial(int, base=2) # 固定了参数base=2
>>> int2('')
64
>>> int2('')
85
作为List 参数的默认元素
max2 = functools.partial(max, 10) max2(5, 6, 7)
# 相当于
args = (10, 5, 6, 7)
max(*args)
与c++对比
字典,元素统计
Python:
def get_counts(sequence):
counts = {}
for x in sequence:
if x in counts:
counts[x] += 1
else:
counts[x] = 1
return counts
c++:map切记元素为空时的陷阱
#include <iostream>
#include <map> using namespace std; int main()
{
cout << "Hello World!" << endl; map<string, int> m; cout << m.size() << endl;
m.insert(std::pair<string,int>("a",100));
cout << m.size() << endl; return ;
}
字典,元素有么?
Python:in & not in
>>> 'a' in count
True
>>> 'a' not in count
False
C++:
iter = m.find(key);
if(iter!=m.end())
{
return iter->second;
}
return null;
排序
C++ sort:
Goto: [c++] Associative Containers
Python sort:
如果是自定义排序呢?
python 字典(dict)的特点就是无序的,按照键(key)来提取相应值(value),
如果我们需要字典按值排序的话,那可以用下面的方法来进行:
(1) 按照value从大到小排序:
dic = {'a':31, 'bc':5, 'c':3, 'asd':4, 'aa':74, 'd':0}
# dic.iteritems() 得到[(键,值)]的列表。
# 然后用sorted方法,通过key这个参数,指定排序是按照value,也就是第2个元素d[1]的值来排序。
# reverse = True表示是需要翻转的,默认是从小到大,翻转的话,那就是从大到小。
dict= sorted(dic.iteritems(), key=lambda d:d[1], reverse = True)
print dict
(2) 对字典按键(key)排序:
dic = {'a':31, 'bc':5, 'c':3, 'asd':4, 'aa':74, 'd':0}
dict= sorted(dic.iteritems(), key=lambda d:d[0]) # 按照第2个元素d[1]的值来排序
print dict
*(3) 遍历则使用:
for (current_state, current_position) in transitions:
# print(current_state, tape[current_position])
new_state, new_bit, direction = transitions[current_state, current_position]
current_state = new_state
tape[current_position] = new_bit
if direction == 'R':
current_position += 1
else:
current_position -= 1
print(tape)
字符串,分割
python :
file = open('multiplication.txt', 'r')
program = file.readlines()
#print(program)
instructions = [line.split() for line in program if not line.startswith('#')] # filter method
#print(instructions)
#for instruction in instructions:
# print(instruction)
transitions = {}
### 1 2 3 4 5
### state, bit, new_state, new_bit, direction
for state, bit, new_state, new_bit, direction in instructions:
print(state, bit, new_state, new_bit, direction)
transitions[state, int(bit) ] = new_state, int(new_bit), direction
### 字典存储好数据,然后打印出来瞧上一瞧
print(transitions)
C++ : http://www.cplusplus.com/faq/sequences/strings/split/专题
The reality is that there are so many ways to split strings it is overwhelming. Here are some, in no particular order, with examples:
|
Method |
Iterated or all-at-once |
Delimiter |
Empty Fields |
||||||
|---|---|---|---|---|---|---|---|---|---|
|
char |
string |
function |
quotable |
offset |
no case |
elided |
trailing |
||
| Boost String Algorithms: Split | all-at-once | Y | Y | Y | 4 | Y | |||
| Boost String Algorithms: Split Regex | all-at-once | 1 | Y | regex | 2 | 2 | 2 | ? | |
| Boost Tokenizer | iterated3 | Y | Y | Y | Y | opt | Y | ||
Trolltech Qt’s QString::split() |
all-at-once | Y | Y | regex | 2 | Y | opt | Y | |
| GNU String Utility Functions: Split | all-at-once | 1 | Y | Y | |||||
iostreams and getline() |
iterated3 | Y | Y | Y | |||||
string::find_first_of() |
all-at-once3 | Y | Y | Y | |||||
strtok() |
iterated3 | 1 | Y | always | NO | ||||
| Roll your own C tokenizer | iterated3 | 1 | Y | opt | Y | ||||
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>
#include <vector> using namespace std;
using namespace boost; void print( vector <string> & v )
{
for (size_t n = ; n < v.size(); n++)
cout << "\"" << v[n] << "\"\n";
cout << endl;
} int main()
{
string s = "a,b, c ,,e,f,";
vector <string> fields; cout << "Original = \"" << s << "\"\n\n"; cout << "Split on \',\' only\n";
split( fields, s, is_any_of( "," ) );
print( fields ); cout << "Split on \" ,\"\n";
split( fields, s, is_any_of( " ," ) );
print( fields ); cout << "Split on \" ,\" and elide delimiters\n";
split( fields, s, is_any_of( " ," ), token_compress_on ); // <---- best
print( fields ); return ;
}
Original = "a,b, c ,,e,f," Split on ',' only
"a"
"b"
" c "
""
"e"
"f"
"" Split on " ,"
"a"
"b"
""
"c"
""
""
"e"
"f"
"" Split on " ," and elide delimiters
"a"
"b"
"c"
"e"
"f"
""
Result
End.
[Python] 00 - Books的更多相关文章
- #001 Python 00号作业:关于课程
请大家继续思考,你希望我们的课程主要涉略哪些方面?你希望我们的课程能够带给你哪些基本的技能?你希望理论课应该怎么上,实验课应该怎么上?对于我们的课程有什么建议或意见 作为一名计算机专业的学生,对于py ...
- 第十三章:Python の 网络编程进阶(二)
本課主題 SQLAlchemy - Core SQLAlchemy - ORM Paramiko 介紹和操作 上下文操作应用 初探堡垒机 SQLAlchemy - Core 连接 URL 通过 cre ...
- python 操作MongoDB
安装MongoDB 启动数据库:安装完成指定数据库存放路径 mongod.exe --dbpath c:\data\db进入目录后运行mongo.exe 成功 创建数据库 > use mydb ...
- 老男孩全栈python学习进程表
老男孩Python高级全栈开发工程师-1 0001.开学典礼_ALEX简介 00:55:53 ☆ 0002.职业生涯_来培训的目的 01:12:29 ☆ 0003.课程目标 00:29: ...
- [Code::Blocks] Install wxWidgets & openCV
The open source, cross platform, free C++ IDE. Code::Blocks is a free C++ IDE built to meet the most ...
- 本人SW知识体系导航 - Programming menu
将感悟心得记于此,重启程序员模式. js, py, c++, java, php 融汇之全栈系列 [Full-stack] 快速上手开发 - React [Full-stack] 状态管理技巧 - R ...
- Redis数据类型和操作
<"Java技术员"成长手册>,包含框架.存储.搜索.优化.分布式等必备知识,都收集在GitHub JavaEgg ,N线互联网开发必备技能兵器谱,欢迎指导 Redis ...
- SQL Server索引进阶:第七级,过滤的索引
原文地址: Stairway to SQL Server Indexes: Level 7,Filtered Indexes 本文是SQL Server索引进阶系列(Stairway to SQL S ...
- django 之MTV模型
一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model(模型):和数据库相关的,负 ...
随机推荐
- 网络编程(1)—TCP
java.net 包中提供了两种常见的网络协议的支持: TCP:TCP 是传输控制协议的缩写,它保障了两个应用程序之间的可靠通信.通常用于互联网协议,被称 TCP / IP. TCP协议: 使用TCP ...
- ios 类别(category)
定义 类别(category)是Objective-C语言的新特性,为现有的类添加新方法的方式.局限性:1.无法添加新的实例变量.2.与类本身的方法名称冲突.当名称冲突时,类别具有更高的优先级.作用: ...
- CSS3 Flex布局整理(三)-项目属性
一.Flex布局中 Flex Item属性控制,可以指定显示顺序.剩余空间的放大,缩小.交叉轴的排列 1.order:定义项目的排列顺序,数值越小,排列越靠前,默认为0.类似z-index 2.fle ...
- Spring---面向切面编程(AOP模块)
Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...
- libnids使用 (转)
http://blog.csdn.net/kl222/article/details/6248827---原始链接 Libnids是一个用于网络入侵检测开发的专业编程接口,它使用了Libpcap所以它 ...
- Google Maps瓦片(tile)地图文件下载(1-11层级)
整理硬盘时,发现一份去年下载的谷歌地图瓦片文件,整理并分享给大家. 地图来源:Google Maps(应该是国内谷歌地图) 采集时间:2017年6月 采集范围:0-6层级世界范围:7-11层级中国范围 ...
- 通过action传过来的值在option获取进行验证
通过action传过来的值在option获取进行验证的方法: for(var i=0;i<document.getElementById("ufacilityType").o ...
- mysql如何处理亿级数据,第一个阶段——优化SQL语句
1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...
- 安装babel遇到的异常
Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure ...
- C#通过DSOFile读取与修改文件的属性
搜了一圈用C#读取与修改文件属性的文章,结果几乎找不到- -: 偶然间看到一个DSOFile工具,然后找到了对该工具进行详细讲解的一篇文章:<DSOfile,一个修改windows系统文件摘要的 ...