在语句中注释目标for无效:
>>> for i: str in test_string:
File "<stdin>", line 1
for i: str in test_string:
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我想知道此时拒绝注释的原因。
一个简单的小问题:
exec("a=3")
print(a)
# This will print 3
Run Code Online (Sandbox Code Playgroud)
如果我使用这个:
def func():
exec("a=3")
print(a)
func()
# NameError: name 'a' is not defined.
Run Code Online (Sandbox Code Playgroud)
发生了什么?我如何exec()在函数中为它赋值?
编辑:我发现了一个有同样问题的问题,但仍然没有解决。
你为什么要这么做?
我知道使用exec()是不好的并且不安全。但是最近我尝试解决OP的问题。我遇到了它。
假设有一个如下所示的函数:
def func(arg1, args2):
# do sth using arg1 and arg2
Run Code Online (Sandbox Code Playgroud)
在运行时,我想继续使用一些args2在定义func.
所以我想做的是:
func_simpler = func(, args2=some_value_for_arg2)
func_simpler(some_value_for_arg1) # actual usage
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?我知道有一种解决方案,例如func更好地定义,但我寻求一种更像是的解决方案func_simpler。提前致谢!
我有一个字符串,其中包含以下序列 '10\S\12/L'
我想根据使用的行拆分字符串,.split('\n')但是它会导致字符串在'\12'. 我发现只要一个字符串包含这个,解释器就会用换行符打印字符串。
为什么和我'\12'一样,'\n'我该如何预防?
在 Python3 中,del是一个像del x. 为什么del不设计成内置函数使用之类的del(x)?
我想获取一个事件或至少在每次input()调用函数时运行该函数。这应该input在不以任何方式包装函数的情况下发生。
有没有办法在input()不直接修改函数的情况下获取事件或某些信号?
我有一个函数get_appendable_values(sequence),它接受一个序列(甚至是空的)并返回可附加到该序列(作为最后一个元素)的所有值的列表。我需要根据此函数中定义的规则并从空序列开始,生成 4 个元素的所有可能序列。
例子 :
假设 的实现get_appendable_values是:
def get_appendable_values(sequence):
'''Dummy rules'''
if len(sequence) == 2:
return [4, 12]
if sequence[-1] == 4:
return [7]
return [0, 9]
Run Code Online (Sandbox Code Playgroud)
预期输出:
[[0, 0, 4, 7],
[0, 0, 12, 0],
[0, 0, 12, 9],
[0, 9, 4, 7],
[0, 9, 12, 0],
[0, 9, 12, 9],
[9, 0, 4, 7],
[9, 0, 12, 0],
[9, 0, 12, 9],
[9, 9, 4, 7],
[9, 9, 12, 0],
[9, 9, 12, 9]] …Run Code Online (Sandbox Code Playgroud) Traceback (most recent call last):
File "c:\users\apu\appdata\local\programs\python\python38\lib\site-packages\IPython\utils\timing.py", line 27, in <module>
import resource
ModuleNotFoundError: No module named 'resource'
Run Code Online (Sandbox Code Playgroud)
在处理上述异常的过程中,又出现了一个异常:
Traceback (most recent call last):
File "c:\users\apu\appdata\local\programs\python\python38\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\apu\appdata\local\programs\python\python38\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\apu\AppData\Local\Programs\Python\Python38\Scripts\jupyter-notebook.EXE\__main__.py", line 7, in <module>
File "c:\users\apu\appdata\local\programs\python\python38\lib\site-packages\jupyter_core\application.py", line 270, in launch_instance
return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
File "c:\users\apu\appdata\local\programs\python\python38\lib\site-packages\traitlets\config\application.py", line 663, in launch_instance
app.initialize(argv)
File "<decorator-gen-7>", line 2, in initialize
File "c:\users\apu\appdata\local\programs\python\python38\lib\site-packages\traitlets\config\application.py", line 87, in catch_config_error
return method(app, *args, …Run Code Online (Sandbox Code Playgroud) 我正在学习 Effective Python(第 2 版)中的第 76 条,但遇到了一个我不理解的案例。具体来说,我不明白为什么要zip消耗其第一个参数的一个额外元素。考虑以下代码:
l1 = []
l2 = [1]
l1_it = iter(l1)
l2_it = iter(l2)
test_it = zip(l2_it, l1_it)
_ = list(test_it):
try:
next(l2_it)
except StopIteration:
print('This should not happen')
Run Code Online (Sandbox Code Playgroud)
这实际上打印This should not happen,我觉得这非常令人惊讶。我希望zip将其第一个参数保留在仍有一个元素要检索的状态。事实是,如果我使用zip(l1_it, l2_it)(即最短的列表在前),那么我确实可以在next(l2_it)不触发异常的情况下调用。
这是预期的吗?
我已经通读了一堆已经回答的问题,但我没有看到这一点——至少我没有意识到。
我正在使用 argparse 来获取一个文件并将其转换为不同的类型。输入文件名是必需的。输出文件名不是必需的,因为可选参数应该处理它。这是到目前为止的代码:
import sys
import argparse
parser = argparse.ArgumentParser(description='Convert file to new type')
parser.add_argument('--json', type=str, help='Converts to json format')
parser.add_argument('--bibtex', type=str, help='Converts to bibtex format')
parser.add_argument('--html', type=str, help='Converts to html format')
parser.add_argument('inputfilename', type=str, help='enter the original filename')
args = parser.parse_args()
filename=args.filename
if args.json:
print('Converting to json ...')
#conversion code here
elif args.bibtex:
print('Converting to bibtex ...')
#conversion code here
elif args.html:
print('Converting to html ...')
#conversion code here
else:
print('No conversion type indicated')
Run Code Online (Sandbox Code Playgroud)
问题是,每当我使用这些标志之一。如果我做
$ ./orsconvert.py --json inputfilename …Run Code Online (Sandbox Code Playgroud) 我有一个很长的 IP 子网数组(+/- 1000 个条目,800 万个 IP),我想检查某些 IP(列表)是否在该数组中。我的代码有效。但速度相当“慢”。由于我必须查找多个 IP 地址,因此我希望搜索速度更快。有没有什么方法可以改善数组的搜索?
数组示例:
nets = [
'192.168.1.0/24',
'192.168.2.0/24',
'192.168.3.0/24',
]
Run Code Online (Sandbox Code Playgroud)
要搜索的代码:
def search(ip_address):
for net in nets:
if ipaddress.ip_address(ip_address) in ipaddress.ip_network(net):
return True
return False
Run Code Online (Sandbox Code Playgroud)