我正在使用Python 3.4.
当我使用IDLE并开始输入时,例如,
my_main_folder = "C:/Us"
在编辑器窗口中,然后按Ctrl + Space,IDLE为我提供了所有子文件夹C:,如果我按下Tab,字符串就完成了"C:/Users".
这意味着IDLE(或Python Shell)能够将字符串识别为路径.
如何让PyCharm(社区版)做到这一点?
我正在使用 Python3 并且刚刚学会了如何使用mypy. 我正在阅读文档(特别是这部分似乎相关),但找不到我的问题的任何答案:
是否可以为类型定义一些快捷方式?
例子:
而不是写作
from typing import List
def f(x: List[int]) -> List[int]:
return x[1:]
Run Code Online (Sandbox Code Playgroud)
我想拥有
from typing import List
sequence = DefineTypeShortcut(List[int])
def f(x: sequence) -> sequence:
return x[1:]
Run Code Online (Sandbox Code Playgroud)
只是为了澄清,我不想定义一个新类Sequence,我只是想要更容易阅读的函数签名。
我有一台装有Windows10和Ubuntu虚拟机的计算机。
log在共享文件夹中有一个文件名称,该文件恰好包含一个Unix EOL字符。它是由虚拟机创建的。
在虚拟机中,发生以下情况:
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.isdir('/media/path/to/log')
True
Run Code Online (Sandbox Code Playgroud)
因此,python错误地认为这log是一个目录(例如,调用list(os.listdir('/media/path/to/log'))引发NotADirectoryError)。
当我插入相对路径时,也会发生同样的情况。
如果我isdir在Windows命令行中调用,它将总是按预期返回False。
我们假设我们有一个字符串:
a = "I would like to go to dinner."
Run Code Online (Sandbox Code Playgroud)
很容易将字符串中的所有内容i和内容更改o为-.如果我们定义:
b = re.sub("i|o","-", a, flags = re.I)
Run Code Online (Sandbox Code Playgroud)
我们得到:
b = "- w-uld l-ke t- g- t- d-nner."
Run Code Online (Sandbox Code Playgroud)
但我不知道如何得到:
"I -o--- -i-- -o -o -o -i-----"
Run Code Online (Sandbox Code Playgroud)
在使用RE模块优雅的方式(注意,最后-在b通过替换点获得).有人可以写
c = ""
for char in a:
c += char if char.lower() in "io" else "-"
Run Code Online (Sandbox Code Playgroud)
但是我想用RE来做这件事.我试着写一些类似的东西
c = re.sub('(?!i|o)', a, flags = re.I)
Run Code Online (Sandbox Code Playgroud)
但结果是
"I- -wo-u-l-d- -li-k-e- -to- -go- -to- -di-n-n-e-r-.-"
Run Code Online (Sandbox Code Playgroud)
而且我不知道为什么.显然,我不明白是如何 …
例如,在图形上编写一些算法时
def f(graph):
#graph is dictionary of pairs vertex_i:{set of edges (i,j)} for 1<=i,j<=n
def g(vertex):
for vertex1 in graph:
do sth
...
for (i,j) in graph[vertex1]:
...
g(j)#recursive call
...
return ...
return g(1)
Run Code Online (Sandbox Code Playgroud)
有限的递归深度有时非常烦人,因为如果必须避免递归,代码会变得更长,更复杂.有没有办法达到无限深度?也许您可以通过以下方法描述问题的一般解决方案
def nthNumber(n):
if n==1: return 1
else: return nthNumber(n-1)+1
Run Code Online (Sandbox Code Playgroud)
(我知道这很简单,请不要回答"你应该只写nthNumber(n):返回n" - 我对一般解决方案感兴趣).感谢帮助!
我注意到Python的GUI被称为Idle.另一方面,Eric Idle是Monty Python的成员.这只是巧合吗?
python ×5
python-3.x ×4
depth ×1
file ×1
infinite ×1
mypy ×1
pycharm ×1
python-idle ×1
recursion ×1
regex ×1
type-hinting ×1