Python 3.x之间是否存在重大差异:
for each_line in data_file:
if each_line.find(":") != -1:
#placeholder for code
#more placeholder
Run Code Online (Sandbox Code Playgroud)
和
for each_line in data:
if not each_line.find(":") == -1:
#placeholder for code
#more placeholder
Run Code Online (Sandbox Code Playgroud)
我的问题不是特别针对上述用法,而是更一般或必要 - 这种语法差异是否以不同的方式工作,即使结果是相同的?有逻辑差异吗?是否存在一个更合适的任务,或者仅仅是风格上的差异?如果这只是风格,Python程序员认为哪一个更干净?
此外,为上述要求相反的实例的区别是什么之间is和==?前者和后者一样,是对象身份和对象价值平等的区别吗?我的意思是,在上面的例子中,是is使用not隐式?
有问题的新手,所以请温柔:
list = [1, 2, 3, 4, 5]
list2 = list
def fxn(list,list2):
for number in list:
print(number)
print(list)
list2.remove(number)
print("after remove list is ", list, " and list 2 is ", list2)
return list, list2
list, list2 = fxn(list, list2)
print("after fxn list is ", list)
print("after fxn list2 is ", list2)
Run Code Online (Sandbox Code Playgroud)
这导致:
1
[1, 2, 3, 4, 5]
after remove list is [2, 3, 4, 5] and list 2 is [2, 3, 4, 5]
3
[2, 3, 4, …Run Code Online (Sandbox Code Playgroud) 有没有相当于enumerateC++中python 的基于范围的循环?我会想象这样的事情.
enumerateLoop (auto counter, auto el, container) {
charges.at(counter) = el[0];
aa.at(counter) = el[1];
}
Run Code Online (Sandbox Code Playgroud)
可以使用模板或宏来完成吗?
我知道我可以使用旧学校循环并迭代直到我到达container.size().但我很感兴趣如何使用模板或宏来解决这个问题.
编辑
在评论中提示后,我使用了增强迭代器.我使用C++ 14获得了另一个有效的解决方案.
template <typename... T>
auto zip(const T &... containers) -> boost::iterator_range<boost::zip_iterator<
decltype(boost::make_tuple(std::begin(containers)...))>> {
auto zip_begin =
boost::make_zip_iterator(boost::make_tuple(std::begin(containers)...));
auto zip_end =
boost::make_zip_iterator(boost::make_tuple(std::end(containers)...));
return boost::make_iterator_range(zip_begin, zip_end);
}
template <typename T>
auto enumerate(const T &container) {
return zip(boost::counting_range(0, static_cast<int>(container.size())),
container);
}
Run Code Online (Sandbox Code Playgroud)
类方法和静态方法之间是否存在速度差异?我知道不同的用例,但有时我可以完全摆脱类方法,并想知道速度差异
现在我有一个图像处理算法,在 Python 中大约有 100 行左右。使用numpy,PIL和大约需要 500 毫秒scipy。我希望让它更快,并且由于实际算法到目前为止似乎已经非常优化,我想知道是否使用不同的方法,例如Cython会改善时间。我相信我可以做几件不同的事情:
numpy这种方式。numpy或那些模块,但仍然非常有效。我只是在这里寻找速度,而不是担心实施的难度。在这种情况下,是否有更好的选择,它们都是一样的,还是值得做?
我有两个独立的功能.每个都需要很长时间才能执行.
def function1(arg):
do_some_stuff_here
return result1
def function2(arg1, arg2, arg3):
do_some_stuff_here
return result2
Run Code Online (Sandbox Code Playgroud)
我想并行启动它们,得到它们的结果(知道哪个是哪个)并在之后处理结果.根据我的理解,多处理比Python 2.7中的线程(GIL相关问题)更有效.但是我有点迷失是否更好地使用Process,Pool或Queue以及如何以正确的pythonic方式为我的用例实现它们.
任何帮助赞赏;)
python parallel-processing python-2.7 python-multiprocessing
我试图使用python从二进制文件中读取一个短和长struct.
但是
print(struct.calcsize("hl")) # o/p 16
Run Code Online (Sandbox Code Playgroud)
这是错误的,它应该是2个字节的短,8个字节的长.我不确定我struct是以错误的方式使用模块.
当我打印每个的值时
print(struct.calcsize("h")) # o/p 2
print(struct.calcsize("l")) # o/p 8
Run Code Online (Sandbox Code Playgroud)
有没有办法强制python保持精度datatypes?
试图提供解决方案的std ::性病string_view和的std :: string :: unordered_set,我与更换玩弄std::unordered_set<std::string>与std::unordered_map<std::string_view, std::unique_ptr<std::string>>(该值std::unique_ptr<std::string>,因为小串的优化将意味着该地址string的底层数据并不总是被转移std::move.
我的原始测试代码似乎有用(省略标题):
using namespace std::literals;
int main(int argc, char **argv) {
std::unordered_map<std::string_view, std::unique_ptr<std::string>> mymap;
for (int i = 1; i < argc; ++i) {
auto to_insert = std::make_unique<std::string>(argv[i]);
mymap.try_emplace(*to_insert, std::move(to_insert));
}
for (auto&& entry : mymap) {
std::cout << entry.first << ": " << entry.second << std::endl;
}
std::cout << std::boolalpha << "\"this\" in map? " << (mymap.count("this") == 1) << std::endl; …Run Code Online (Sandbox Code Playgroud) 我有一个示例函数:
from typing import Dict
def cool_func(foo: Dict[object, str], bar: bool, baz: float) -> None:
foo_type_error_msg = "foo must be a Dict[object, str]"
if not foo.isinstance(dict):
raise TypeError(foo_type_error_msg)
for s in foo.values():
if not s.isinstance(str):
raise TypeError(foo_type_error_msg)
# the rest of the function
Run Code Online (Sandbox Code Playgroud)
如何使用类型提示中指定的类型在运行时检查变量?请注意,我不想对其他变量严格要求(只需检查参数foo)。
如果您想知道,我想强制执行该类型,以便错误(由错误类型引起的)不会出现在某处,the rest of the function并使用户与默认错误消息混淆。
我希望我能拥有这样的东西:
def cool_func(foo: Dict[object, str], bar: bool, baz: float) -> None:
check_type('foo')
# the rest of the function
Run Code Online (Sandbox Code Playgroud)
或者至少
def cool_func(foo: Dict[object, str], bar: bool, baz: float) …Run Code Online (Sandbox Code Playgroud) 注意:这是对以下问题的后续问题:当编译时已知的引用在结构中占用空间时,这是否是错过的优化?,这表明聚合初始化可以通过将其初始化b为a对其他变量的引用来代替作为引用的默认初始化。这个问题是关于不可能进行初始化的情况。
请参阅以下示例:
struct Foo {
int a;
int &b;
Foo() : b(a) { }
};
Run Code Online (Sandbox Code Playgroud)
如果错过了优化sizeof(Foo)!=sizeof(int)吗?
我的意思是,编译器可以b从其始终引用的结构中删除它a吗?
有什么阻止编译器进行这种转换的吗?
(请注意,struct Foo看起来是这样。没有其他构造函数,等等。但是您可以在周围添加任何内容Foo,这表明此优化将违反标准。)
python ×7
c++ ×3
struct ×2
alias ×1
assert ×1
c++14 ×1
c++17 ×1
class-method ×1
ctypes ×1
cython ×1
enumerate ×1
list ×1
optimization ×1
padding ×1
python-2.7 ×1
reference ×1
sizeof ×1
string-view ×1
syntax ×1
type-hinting ×1
types ×1