小编eca*_*mur的帖子

确定数组中的重复值

假设我有一个数组

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 …

python numpy unique duplicates

51
推荐指数
7
解决办法
4万
查看次数

Git push remote被拒绝{change ### closed}

由于此错误,我在将更改从本地主服务器推送到远程主服务器时遇到问题:

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.

git gerrit

39
推荐指数
5
解决办法
7万
查看次数

为"make_function"推断lambda或任意callable的调用签名

在某些情况下,希望能够键入 - 擦除可调用的(例如函数,函数指针,带有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很好.


编辑:回答我自己的问题.

c++ lambda type-inference function c++11

26
推荐指数
1
解决办法
3945
查看次数

contextlib.nested的替代方法,具有可变数量的上下文管理器

我们有代码根据运行时参数调用可变数量的上下文管理器:

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在将来的某个时间将其删除?

python with-statement deprecated contextmanager

25
推荐指数
2
解决办法
6172
查看次数

为什么在 C++20 中允许这个值向下转换(static_cast 到对象类型)?

令我惊讶的是,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 中合法,如果是,为什么?

c++ downcast static-cast language-lawyer c++20

19
推荐指数
1
解决办法
582
查看次数

在没有参数的情况下调用时,为什么省略号首选为可变参数模板?

我正在使用以下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++ templates sfinae variadic-templates c++11

16
推荐指数
1
解决办法
1999
查看次数

为什么make_optional会衰减其参数类型?

(可能不是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转换?

c++ optional type-traits c++-standard-library c++14

16
推荐指数
3
解决办法
1825
查看次数

移动构造函数被逗号运算符抑制

这个程序:

#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 - …

c++ return comma-operator move-semantics c++11

13
推荐指数
2
解决办法
382
查看次数

为什么这个 C 风格的转换不考虑 static_cast 后跟 const_cast?

考虑:

float const& f = 5.9e-44f;
int const i = (int&) f;
Run Code Online (Sandbox Code Playgroud)

根据expr.cast/4这应该被视为,为了:

  • const_­cast
  • static_­cast
  • astatic_­cast后跟 a const_­cast,
  • reinterpret_­cast,或
  • areinterpret_­cast后跟 a const_­cast,

显然 astatic_­cast<int const&>后跟 aconst_­cast<int&>可行的,并且将导致int值为0 的 a。但是所有编译器都初始化i42,表明它们采用了最后一个选项reinterpret_­cast<int const&>后跟const_­cast<int&>。为什么?

相关:在 C++ 中,C 风格的强制转换可以调用转换函数然后抛弃常量吗?,为什么 (int&)0 格式错误?, C++ 规范是否说明了如何在 static_cast/const_cast 链中选择类型以用于 C 样式强制转换?,用 (float&)int …

c++ casting language-lawyer reinterpret-cast

11
推荐指数
1
解决办法
190
查看次数

在列表中迭代(item,others)

假设我有一个列表:

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)

理想情况下,我正在寻找一个简洁的表达,我可以在理解中使用.

python sequence complement python-itertools

10
推荐指数
1
解决办法
196
查看次数