假设我有一个数组
a = np.array([1, 2, 1, 3, 3, 3, 0])
Run Code Online (Sandbox Code Playgroud)
我怎样(有效地,Python地)找到哪些元素a是重复的(即,非唯一值)?在这种情况下,结果将是array([1, 3, 3])或可能array([1, 3])是有效的.
我想出了一些似乎有用的方法:
m = np.zeros_like(a, dtype=bool)
m[np.unique(a, return_index=True)[1]] = True
a[~m]
Run Code Online (Sandbox Code Playgroud)
a[~np.in1d(np.arange(len(a)), np.unique(a, return_index=True)[1], assume_unique=True)]
Run Code Online (Sandbox Code Playgroud)
这个很可爱,但可能是非法的(a实际上并不是唯一的):
np.setxor1d(a, np.unique(a), assume_unique=True)
Run Code Online (Sandbox Code Playgroud)
u, i = np.unique(a, return_inverse=True)
u[np.bincount(i) > 1]
Run Code Online (Sandbox Code Playgroud)
s = np.sort(a, axis=None)
s[:-1][s[1:] == s[:-1]]
Run Code Online (Sandbox Code Playgroud)
s = pd.Series(a)
s[s.duplicated()]
Run Code Online (Sandbox Code Playgroud)
有什么我错过的吗?我不一定会寻找一个只有numpy的解决方案,但它必须使用numpy数据类型,并且对中型数据集(高达1000万的大小)有效.
使用1000万大小的数据集(在2.8GHz Xeon上)进行测试:
a = np.random.randint(10**7, size=10**7)
Run Code Online (Sandbox Code Playgroud)
排序最快的是1.1秒.可疑的xor1d是,在第二2.6S,其次是屏蔽和熊猫Series.duplicated在3.1s,bincount在5.6s,并in1d …
由于此错误,我在将更改从本地主服务器推送到远程主服务器时遇到问题:
remote: Processing changes: refs: 1, done
To ssh://xxxxx@gerrit.dev.xxxxx.net:29418/xxxxxx
! [remote rejected] HEAD -> refs/for/master (change 14823 closed)
error: failed to push some refs to 'ssh://xxxxx@gerrit.dev.xxxxx.net:29418/xxxxxx'
Run Code Online (Sandbox Code Playgroud)
任何想法如何解决这个问题?
git status表示我的分支在5个提交之前超过origin/master.
在某些情况下,希望能够键入 - 擦除可调用的(例如函数,函数指针,带有operator()lambda的对象实例mem_fn),例如在使用带有C++ 11 lambdas的Boost适配器中,其中可复制的和可默认构造的类型是必需的.
std::function将是理想的,但似乎没有办法自动确定用于实例化类模板的签名std::function.是否有一种简单的方法来获取任意可调用的函数签名和/或将其包装在适当的std::function实例化实例(即make_function函数模板)中?
具体来说,我正在寻找一个或另一个
template<typename F> using get_signature = ...;
template<typename F> std::function<get_signature<F>> make_function(F &&f) { ... }
Run Code Online (Sandbox Code Playgroud)
这样make_function([](int i) { return 0; })返回std::function<int(int)>.显然,如果一个实例可以使用多个签名(例如,具有多个,模板或默认参数operator()的对象)进行调用,那么预计这不会起作用.
尽管非过度复杂的非Boost解决方案是优选的,但Boost很好.
编辑:回答我自己的问题.
我们有代码根据运行时参数调用可变数量的上下文管理器:
from contextlib import nested, contextmanager
@contextmanager
def my_context(arg):
print("entering", arg)
try:
yield arg
finally:
print("exiting", arg)
def my_fn(items):
with nested(*(my_context(arg) for arg in items)) as managers:
print("processing under", managers)
my_fn(range(3))
Run Code Online (Sandbox Code Playgroud)
但是,contextlib.nested自Python 2.7以来已弃用:
DeprecationWarning: With-statements now directly support multiple context managers
Run Code Online (Sandbox Code Playgroud)
Python'with '语句中的多个变量的答案表明contextlib.nested存在一些"令人困惑的容易出错的怪癖",但建议使用多管理器with语句的替代方法不适用于可变数量的上下文管理器(并且还会破坏向后兼容性) ).
是否有任何替代品contextlib.nested不被弃用,并且(最好)没有相同的错误?
或者我应该继续使用contextlib.nested并忽略警告?如果是这样,我是否应该计划contextlib.nested在将来的某个时间将其删除?
令我惊讶的是,gcc 11.2接受此代码,但仅在 C++20 模式下:
struct Base {};
struct Derived : Base { int i; };
int f(Base& b) { return static_cast<Derived>(b).i; }
// ^~~~~~~ oops, forgot the `&`
Run Code Online (Sandbox Code Playgroud)
同样,MSVC 19.29 在 C++20 模式下接受它,在 C++17 模式下拒绝它,但即使在 C++20 模式下,clang 也会拒绝它。
查看汇编器输出,f总是返回0,因此它忽略了Derived传递给的任何潜在的实际数据f。
这是 UB,还是在 C++20 中合法,如果是,为什么?
我正在使用以下SFINAE模式来评估可变参数类型列表上的谓词:
#include <type_traits>
void f(int = 0); // for example
template<typename... T,
typename = decltype(f(std::declval<T>()...))>
std::true_type check(T &&...);
std::false_type check(...);
template<typename... T> using Predicate = decltype(check(std::declval<T>()...));
static_assert(!Predicate<int, int>::value, "!!");
static_assert( Predicate<int>::value, "!!");
static_assert( Predicate<>::value, "!!"); // fails
int main() {
}
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,选择省略号过载时check被调用空的参数列表,因此Predicate<>是std::false_type即使SFINAE表达是有效的!
variadic函数模板不应该总是优先于省略号函数吗?
有没有解决方法?
(可能不是C++ 14,可能是库TS)工具make_optional被定义为(在n3672中):
template <class T>
constexpr optional<typename decay<T>::type> make_optional(T&& v) {
return optional<typename decay<T>::type>(std::forward<T>(v));
}
Run Code Online (Sandbox Code Playgroud)
为什么有必要改变类型T(即不仅仅是返回optional<T>),是否有一种哲学(以及实际)理由用于decay转换?
这个程序:
#include <iostream>
struct T {
T() {}
T(const T &) { std::cout << "copy constructor "; }
T(T &&) { std::cout << "move constructor "; }
};
int main() {
([](T t) -> T { return t; })({}); std::cout << '\n';
([](T t) -> T { return void(), t; })({}); std::cout << '\n';
([](T t) -> T { return void(), std::move(t); })({}); std::cout << '\n';
}
Run Code Online (Sandbox Code Playgroud)
当由gcc-4.7.1输出编译时(链接):
move constructor
copy constructor
move constructor
Run Code Online (Sandbox Code Playgroud)
为什么逗号运算符有这种效果?标准说:
5.18逗号运算符[expr.comma]
1 - …
考虑:
float const& f = 5.9e-44f;
int const i = (int&) f;
Run Code Online (Sandbox Code Playgroud)
根据expr.cast/4这应该被视为,为了:
- 一
const_cast,- 一
static_cast,- a
static_cast后跟 aconst_cast,- 一
reinterpret_cast,或- a
reinterpret_cast后跟 aconst_cast,
显然 astatic_cast<int const&>后跟 aconst_cast<int&>是可行的,并且将导致int值为0 的 a。但是所有编译器都初始化i为42,表明它们采用了最后一个选项reinterpret_cast<int const&>后跟const_cast<int&>。为什么?
相关:在 C++ 中,C 风格的强制转换可以调用转换函数然后抛弃常量吗?,为什么 (int&)0 格式错误?, C++ 规范是否说明了如何在 static_cast/const_cast 链中选择类型以用于 C 样式强制转换?,用 (float&)int …
假设我有一个列表:
l = [0, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
如何迭代列表,从列表中获取每个项目及其补充?那是,
for item, others in ...
print(item, others)
Run Code Online (Sandbox Code Playgroud)
会打印
0 [1, 2, 3]
1 [0, 2, 3]
2 [0, 1, 3]
3 [0, 1, 2]
Run Code Online (Sandbox Code Playgroud)
理想情况下,我正在寻找一个简洁的表达,我可以在理解中使用.
c++ ×6
c++11 ×3
python ×3
c++14 ×1
c++20 ×1
casting ×1
complement ×1
deprecated ×1
downcast ×1
duplicates ×1
function ×1
gerrit ×1
git ×1
lambda ×1
numpy ×1
optional ×1
return ×1
sequence ×1
sfinae ×1
static-cast ×1
templates ×1
type-traits ×1
unique ×1