PyCharm的一些使用技巧

  1. 定位到函数定义 
    在函数名处 Ctrl + B 
    就会快速定位到函数定义处

  2. 在Console中执行文件 
    全选内容后,右键菜单 
    Execute Selection in Console 
    或者快捷键 Alt + Shift + E

  3. 设置工作目录,这样一些数据文件可以用文件名快速访问

<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>
  1. 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>
  1. 如何读取含有 环境变量的路径 : 
    使用os.path.expandvars函数 
    os.path.expandvars(r”%windir%\fonts\simsun.ttc”) 
    参考如下: 
    Python 3.5 
    https://docs.python.org/3.5/library/os.path.html

  2. 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
  1. 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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值