小编Dem*_*unt的帖子

Python DataFrame或用于存储对象的列表

我可以像在列表中那样"存储"pandas/numpy Series-DataFrame/ndarray中的类实例吗?或者这些库支持内置类型(数字,字符串).

例如,我有Pointx,y坐标,我想存储PointsPlane,这将返回Point与给定的坐标.

#my class
class MyPoint:

    def __init__(self, x,y):
        self.x = x
        self.y = y

    @property
    def x(self):
        return self.x

    @property
    def y(self):
        return self.y
Run Code Online (Sandbox Code Playgroud)

在这里我创建实例:

first_point = MyClass(1,1)
second_point = MyClass(2,2)
Run Code Online (Sandbox Code Playgroud)

我可以将实例存储在某个列表中

my_list = []
my_list.append(first_point)
my_list.append(second_point)
Run Code Online (Sandbox Code Playgroud)

列表中的问题是它的索引与x,y属性不对应.

字典/ DataFrame方法:

Plane = {"x" : [first_point.x, second_point.x], "y" : [first_point.y, second_point.y], "some_reference/id_to_point_instance" = ???}
Plane_pd = pd.DataFrame(Plane)
Run Code Online (Sandbox Code Playgroud)

我读过帖子,使用实例的"id"作为DataFrame中的第三列值可能会导致垃圾收集器出现问题.

python numpy pandas

9
推荐指数
1
解决办法
5895
查看次数

模板类方法的部分专业化或实例化

我有几个模板参数的模板结构

template<class Result, class T, class K>
struct MyClass
{
public:
    Result foo()
    {
        return Result{};
    }
};
Run Code Online (Sandbox Code Playgroud)

此结构对所有模板都适用,但Result为空时除外。我知道,Result{}不能将其实现为void类型,所以我当前的解决方案是使用部分专业化,如下所示:

template<class T, class K>
struct MyClass<void, T, K>
{
public:
    void foo()
    {
        return;
    }
};
Run Code Online (Sandbox Code Playgroud)

这允许执行以下操作:

int main()
{
    MyClass<void, double, char> mycl1;
    MyClass<int, double, char> mycl2;

    mycl1.foo();
    mycl2.foo();
}
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以使mycl1.foo()C ++ 14标准中没有部分类专门化的情况进行编译?我可以使用if constexr和键入特征is_void_v组合,但是我想查找是否有以下方法:

  • 模板类方法的专业化部分显式

  • 模板类方法的实例化

c++ templates

7
推荐指数
1
解决办法
105
查看次数

R语言,暂停循环并要求用户继续

我有一个想法在某些迭代中暂停循环并向“用户”询问一些答案。

例如

some_value = 0
some_criteria = 50
for(i in 1:100)
{
  some_value = some_value + i
  if(some_value > some_criteria)
  {
    #Here i need to inform the user that some_value reached some_criteria
    #I also need to ask the user whether s/he wants to continue operations until the loop ends
    #or even set new criteria
  }
}
Run Code Online (Sandbox Code Playgroud)

再次,我想暂停循环,并询问用户是否愿意继续,例如:“按 Y/N”

loops r pause

5
推荐指数
1
解决办法
4774
查看次数

碳增加到下周一

我有Carbon日期变量。

Carbon::parse("2018-08-01") //tuesday
Run Code Online (Sandbox Code Playgroud)

我想增加几天直到下一个monday ("2018-08-07")

有没有像这样的命令

 Carbon->addDaysUntil("monday"); ->addMonthUntil("september")
Run Code Online (Sandbox Code Playgroud)

等等。

所以我想将当前日期更改为下周,月份,年份的开始

php php-carbon

4
推荐指数
2
解决办法
4423
查看次数

使用字典过滤DataFrame

我是熊猫和蟒蛇的新手。我想用字典过滤DataFrame

import pandas as pd
from pandas import DataFrame

df = DataFrame({'A': [1, 2, 3, 3, 3, 3], 'B': ['a', 'b', 'f', 'c', 'e', 'c'], 'D':[0,0,0,0,0,0]})
my_filter = {'A':[3], 'B':['c']}
Run Code Online (Sandbox Code Playgroud)

当我打电话

df[df.isin(my_filter)]
Run Code Online (Sandbox Code Playgroud)

我得到

     A    B   D
0  NaN  NaN NaN
1  NaN  NaN NaN
2  3.0  NaN NaN
3  3.0    c NaN
4  3.0  NaN NaN
5  3.0    c NaN
Run Code Online (Sandbox Code Playgroud)

我想要的是

     A    B   D
3  3.0    c   0
5  3.0    c   0
Run Code Online (Sandbox Code Playgroud)

我不想在字典中添加“D”,我想获取在 A 和 B 中具有正确值的行

python dataframe pandas

3
推荐指数
1
解决办法
3809
查看次数

ansible 看不到主机(无法访问)

我最近安装了ansible

demaunt@demaunt-pc:~$ ansible --version
ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
Run Code Online (Sandbox Code Playgroud)

我添加了 2 行到:/etc/ansible/hosts

[local]
192.168.1.102
Run Code Online (Sandbox Code Playgroud)

并更改ansible.cfg注释的 1 行:

# uncomment this to disable SSH key host checking
host_key_checking = False
Run Code Online (Sandbox Code Playgroud)

当我这样做时:

ansible all -m ping
192.168.1.102 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.1.102' …
Run Code Online (Sandbox Code Playgroud)

ansible

2
推荐指数
1
解决办法
1377
查看次数

模板函数中的多态

我想使用模板函数来处理多态类和非多态类。这是3个基本课程。

class NotDerived
{

};

class Base
{
public:
    virtual ~Base() {}
    void base_method() {}
};

class Derived : public Base
{

};
Run Code Online (Sandbox Code Playgroud)

由于NotDerived没有虚函数,所以我不能使用dynamic_cast,接下来是模板函数:

template<class T>
auto foo(T& some_instance)
{
    if (std::is_base_of_v<Base, T>)
    {
        //CASE_1: Works for d and b
        /*some_instance.base_method();*/

        //CASE_2: Works for d and b
        /*auto lamb1 = [](T& some_instance) {some_instance.base_method(); };
        lamb1(some_instance);*/


        auto lamb2 = [](T& some_instance) {((Base&)some_instance).base_method(); };
        lamb2(some_instance);
    }
}
Run Code Online (Sandbox Code Playgroud)

主要功能是这样的:

void main()
{
    Derived d{};
    Base b{};
    NotDerived nd{};

    foo(d);
    foo(b);
    foo(nd); …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism templates

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

在一个函数中使用*arg和可选参数

我怎样才能将*arg和变量与defaull值一起使用?

def my_fun(a, b = True, *arg):
    print (a)
    print (b)
    print (arg)

my_fun(1,2,3)
Run Code Online (Sandbox Code Playgroud)

我想得到的是

1
True
(2,3,)
Run Code Online (Sandbox Code Playgroud)

我得到的是

1
2
(3)
Run Code Online (Sandbox Code Playgroud)

python arguments

0
推荐指数
1
解决办法
69
查看次数

无法将std :: function移入地图

我有一些信号原型类

#include <map>
#include <string>
#include <functional>

template <class T>
class Signal
{
public:
    std::function<T> slot;
};
Run Code Online (Sandbox Code Playgroud)

接下来是模板单例SignalCollection类,该类自动为Signal

template <class T>
class SignalCollection
{
private:
    SignalCollection() {}
    SignalCollection(const SignalCollection&) = delete;
    SignalCollection& operator= (const SignalCollection&) = delete;
public:

    static SignalCollection& Instance()
    {
        static SignalCollection br{};
        return br;
    }

    std::map<std::string, T> signals_map;

    void add(T&& signal)
    {
        this->signals_map.insert(std::make_pair("a", std::forward<T>(signal)));
    }
};
Run Code Online (Sandbox Code Playgroud)

最后,我有一个函数可以推断SignalCollection某些类型Signal

template<class T>
auto& get_collection_for_signal(T&& t)
{
    return SignalCollection<T>::Instance();
}
Run Code Online (Sandbox Code Playgroud)

问题是,我无法将值添加到集合图。这是主要的:

void foo()
{

} …
Run Code Online (Sandbox Code Playgroud)

c++ templates move-semantics

0
推荐指数
1
解决办法
74
查看次数