-
定位到函数定义
在函数名处 Ctrl + B
就会快速定位到函数定义处
-
在Console中执行文件
全选内容后,右键菜单
Execute Selection in Console
或者快捷键 Alt + Shift + E
-
设置工作目录,这样一些数据文件可以用文件名快速访问
<code class="hljs python has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-prompt" style="color:#06666;box-sizing: border-box;">>>> </span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">import</span> os
<span class="hljs-prompt" style="color:#06666;box-sizing: border-box;">>>> </span>os.chdir(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">'C:\\GitHubRepositories\\PythonProjects\\ML2'</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
4.设置编译目录,可以直接在PythonConsole中load相关文件
<code class="hljs tex has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">sys.path.extend(<span class="hljs-special" style="color:#66660;box-sizing: border-box;">[</span>'C:<span class="hljs-command" style="color:#0088;box-sizing: border-box;">\\</span>GitHubRepositories<span class="hljs-command" style="color:#0088;box-sizing: border-box;">\\</span>PythonProjects<span class="hljs-command" style="color:#0088;box-sizing: border-box;">\\</span>ML2'<span class="hljs-special" style="color:#66660;box-sizing: border-box;">]</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
- Python3.5 已经不能直接reload, 必须使用如下:
<code class="hljs python has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-prompt" style="color:#06666;box-sizing: border-box;">>>> </span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">import</span> importlib
<span class="hljs-prompt" style="color:#06666;box-sizing: border-box;">>>> </span>importlib.reload(kNN)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
-
如何读取含有 环境变量的路径 :
使用os.path.expandvars函数
os.path.expandvars(r”%windir%\fonts\simsun.ttc”)
参考如下:
Python 3.5
https://docs.python.org/3.5/library/os.path.html
-
string.format:
一个例子:
将数字转化为一个颜色
<code class="hljs rsl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-string" style="color:#0880;box-sizing: border-box;">"#{0:06X}"</span>.<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">format</span>(<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">random</span>.randint(<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>, <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0xFFFFFF</span>))</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
引用:
https://docs.python.org/3/library/string.html
- Python 3.5 Annotation 在PyCharm中的应用
注意在表示参数是 tuple,list时要先:
from typing import Tuple
from typing import List
随后写如下语句:
def file_to_matrix(a_filename: str)-> Tuple[ndarray, list, dict, List[str]]:
而不能写
def file_to_matrix(a_filename: str)-> tuple[ndarray, list, dict, list[str]]:
否则会报错:
def file_to_matrix(a_filename: str)-> tuple[ndarray, list, dict, List[str]]:
TypeError: ‘type’ object is not subscriptable
http://blog.jetbrains.com/pycharm/2015/11/python-3-5-type-hinting-in-pycharm-5/
https://docs.python.org/3/library/typing.html