假设我想使用 jupiter notebook/ipython 作为开发环境,然后将所有内容复制到 python 脚本中。在 ipython 中我们有这样的命令
In [1]: cd ..
/Users/myname/Desktop/software
In [2]: ls
blah_blah_blah/
Run Code Online (Sandbox Code Playgroud)
假设我完成了 ipython 笔记本并想要复制所有内容(假设我有 1000 行并且我无法将它们一一编辑)来创建我的 python 脚本。是否可以让我的 python 脚本理解“cd ..”等行?
现在我有一个文件名列表。我想打印它们,逗号后不带空格,也不带引号。
所以基本上我有一个具有以下输出的文件:
['1', '2', '3']
Run Code Online (Sandbox Code Playgroud)
我希望输出是
1,2,3
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Autokey-py3 v0.93.10(在 Linux Mint 18.2 中),使用命令生成 Unicode 字符keyboard.send_keys。不幸的是,以下尝试均无效。
keyboard.sendkeys("\xe2\x80\x94")\nkeyboard.sendkeys(u"\\u2014")\nRun Code Online (Sandbox Code Playgroud)\n或者从 unicode.py 复制此尝试:
\nimport.paste_character("\xe2\x80\x94")\nRun Code Online (Sandbox Code Playgroud)\n有人可以告诉我我缺少什么吗?
\n根据我的编程语言课程,在使用词法范围的语言中
函数体是在定义函数的环境中计算的,而不是在调用函数的环境中。
例如,SML 遵循以下行为:
val x = 1
fun myfun () =
x
val x = 10
val res = myfun() (* res is 1 since x = 1 when myfun is defined *)
Run Code Online (Sandbox Code Playgroud)
另一方面,Python 不遵循这种行为:
x = 1
def myfun():
return x
x = 10
myfun() # 10 since x = 10 when myfun is called
Run Code Online (Sandbox Code Playgroud)
那么为什么Python 被描述为使用词法作用域呢?
import ast
code = '1+1'
expr = ast.parse(code).body[0]
print(type(expr))
compile(ast.Expression(expr), 'string', "eval")
Run Code Online (Sandbox Code Playgroud)
让我明白
import ast
code = '1+1'
expr = ast.parse(code).body[0]
print(type(expr))
compile(ast.Expression(expr), 'string', "eval")
Run Code Online (Sandbox Code Playgroud)
compile(expr, '<string>', "eval")
Run Code Online (Sandbox Code Playgroud)
也不起作用:
TypeError: expected Expression node, got Expr
Run Code Online (Sandbox Code Playgroud) 我有一个set如下所示的列表。我想编写一个函数来返回在这些集合中只出现一次的元素。我写的函数有点工作。我想知道,有没有更好的方法来处理这个问题?
s1 = {1, 2, 3, 4}
s2 = {1, 3, 4}
s3 = {1, 4}
s4 = {3, 4}
s5 = {1, 4, 5}
s = [s1, s2, s3, s4, s5]
def unique(s):
temp = []
for i in s:
temp.extend(list(i))
c = Counter(temp)
result = set()
for k,v in c.items():
if v == 1:
result.add(k)
return result
unique(s) # will return {2, 5}
Run Code Online (Sandbox Code Playgroud) 我使用下面的代码阅读文本格式,
f = open("document.txt", "r+", encoding='utf-8-sig')
f.read()
Run Code Online (Sandbox Code Playgroud)
但类型f是_io.TextIOWrapper. 但我需要输入字符串才能继续。
请帮我转换 _io.TextIOWrapper为字符串。
我正在尝试__new__在 Python 的元类中键入该方法,以便它使 mypy 满意。代码将是这样的(取自pep-3115 - “Python 3000 中的元类”并精简了一点):
from __future__ import annotations
from typing import Type
# The metaclass
class MetaClass(type):
# The metaclass invocation
def __new__(cls: Type[type], name: str, bases: tuple, classdict: dict) -> type:
result = type.__new__(cls, name, bases, classdict)
print('in __new__')
return result
class MyClass(metaclass=MetaClass):
pass
Run Code Online (Sandbox Code Playgroud)
有了这个,mypy 抱怨说,Incompatible return type for "__new__" (returns "type", but must return a subtype of "MetaClass"),指着线def __new__。
我也试过:
def __new__(cls: Type[MetaClass], name: str, bases: …Run Code Online (Sandbox Code Playgroud) 我有一个包含字符串的列表,如下所示。
candidates = ["Hello", "World", "HelloWorld", "Foo", "bar", "ar"]
Run Code Online (Sandbox Code Playgroud)
我希望列表被过滤为["HelloWorld", "Foo", "Bar"],因为其他的是子字符串。我可以这样做,但不认为它很快或优雅。
def filter_not_substring(candidates):
survive = []
for a in candidates:
for b in candidates:
if a == b:
continue
if a in b:
break
else:
survive.append(a)
return survive
Run Code Online (Sandbox Code Playgroud)
有什么快速的方法可以做到吗?
如何在自定义 TKinter 窗口中添加 Manu 栏?
我想在我的自定义 TKinter 窗口中添加一个菜单栏。