蟒蛇 3.6
我想从字符串中删除字符串列表。这是我第一次糟糕的尝试:
string = 'this is a test string'
items_to_remove = ['this', 'is', 'a', 'string']
result = list(filter(lambda x: x not in items_to_remove, string.split(' ')))
print(result)
Run Code Online (Sandbox Code Playgroud)
输出:
['test']
Run Code Online (Sandbox Code Playgroud)
但这不起作用,如果 x间隔不合适,。我觉得一定有内置解决方案,嗯一定有更好的方法!
我看过这个关于堆栈溢出的讨论,我的确切问题......
不要浪费我的努力。我为所有解决方案计时。我相信最简单、最快和最 Pythonic 的是简单的 for 循环。这不是另一个帖子中的结论......
result = string
for i in items_to_remove:
result = result.replace(i,'')
Run Code Online (Sandbox Code Playgroud)
测试代码:
import timeit
t1 = timeit.timeit('''
string = 'this is a test string'
items_to_remove = ['this', 'is', 'a', 'string']
result = list(filter(lambda x: x not in items_to_remove, string.split(' ')))
''', …Run Code Online (Sandbox Code Playgroud) Python 3.6
我正在尝试创建一个装饰器,它自动将参数的字符串指定为默认值.
如:
def example(one='one', two='two', three='three'):
pass
Run Code Online (Sandbox Code Playgroud)
相当于:
@DefaultArguments
def example(one, two, three):
pass
Run Code Online (Sandbox Code Playgroud)
这是我的尝试(不起作用..但..)DefaultArguments:
from inspect import Parameter, Signature, signature
class DefaultArguments(object):
@staticmethod
def default_signature(signature):
def default(param):
if param.kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.POSITIONAL_ONLY):
return param.replace(default=param.name)
else:
return param
return Signature([default(param) for param in signature.parameters.values()])
def __init__(self, func):
self.func = func
self.sig = self.default_signature(signature(func))
def __call__(self, *args, **kwargs):
arguments = self.sig.bind(*args, **kwargs)
return self.func(arguments)
Run Code Online (Sandbox Code Playgroud)
static方法default_signature为函数创建了所需的签名,但是我很难将新签名分配给函数.我正在尝试使用Signature.bind我已经阅读了文档,但我错过了一些东西.
编辑
结合Ashwini Chaudhary的回答:
from inspect import …Run Code Online (Sandbox Code Playgroud) 我想使用https协议从服务器下载文件。我应该怎么做呢?这是我使用http的基本代码
response=requests.get('http://url',stream='True')
handle=open('dest_file.txt','wb')
for chunk in response.iter_content(chunk_size=512):
if chunk: # filter out keep-alive new chunks
handle.write(chunk)
handle.close()
Run Code Online (Sandbox Code Playgroud)
请求模块也可以用于https吗?
我正在尝试从postgres_types文档中运行 Rust 代码。
示例代码可以在这里找到:postgres_types
我的锈环境:
货物 --version 货物 1.40.0-nightly (5da4b4d47 2019-10-28)
rustc --version rustc 1.40.0-nightly (b520af6fd 2019-11-03)
主文件
#[cfg(feature = "derive")]
use postgres_types::{ToSql, FromSql};
#[derive(Debug, ToSql, FromSql)]
enum Mood {
Sad,
Ok,
Happy,
}
fn main() {
let mood = Mood::Sad;
println!("{:?}", mood);
}
Run Code Online (Sandbox Code Playgroud)
Cargo.toml
...
[dependencies]
postgres-types = "0.1.0-alpha.1"
Run Code Online (Sandbox Code Playgroud)
当我尝试运行时,cargo run我得到:
error: cannot find derive macro `ToSql` in this scope
--> src\main.rs:4:17
|
4 | #[derive(Debug, ToSql, FromSql)]
| ^^^^^
error: cannot find derive …Run Code Online (Sandbox Code Playgroud) Ubuntu 18.04.2 LTS 上的 Python 3.7.1
使用 Pycharm 版本:
PyCharm 2019.1.3 (Professional Edition)
Build #PY-191.7479.30, built on May 30, 2019
Linux 4.18.0-22-generic
Run Code Online (Sandbox Code Playgroud)
我的os.get_terminal_size()函数调用有问题
从终端窗口运行命令有效:
Python 3.7.1 (default, Oct 22 2018, 11:21:55)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.get_terminal_size()
os.terminal_size(columns=223, lines=18)
Run Code Online (Sandbox Code Playgroud)
但是从 Python 控制台窗口运行它不会
>>>import os
>>>os.get_terminal_size()
Traceback (most recent call last):
File "<input>", line 1, in <module>
OSError: [Errno 25] Inappropriate ioctl for device
Run Code Online (Sandbox Code Playgroud)
我的谷歌搜索没有产生太多针对我手头问题的信息。OSError: [Errno …
我想分配0给二维数组的不同长度切片。
例子:
import numpy as np
arr = np.array([[1,2,3,4],
[1,2,3,4],
[1,2,3,4],
[1,2,3,4]])
idxs = np.array([0,1,2,0])
Run Code Online (Sandbox Code Playgroud)
鉴于上述数组arr和索引idxs,您如何分配不同长度的切片。结果是:
arr = np.array([[0,2,3,4],
[0,0,3,4],
[0,0,0,4],
[0,2,3,4]])
Run Code Online (Sandbox Code Playgroud)
这些不起作用
slices = np.array([np.arange(i) for i in idxs])
arr[slices] = 0
Run Code Online (Sandbox Code Playgroud)
arr[:, :idxs] = 0
Run Code Online (Sandbox Code Playgroud)