小编Kri*_*ula的帖子

熊猫读取csv忽略最后一列的结尾分号

我的数据文件如下所示:

data.txt
user,activity,timestamp,x-axis,y-axis,z-axis
0,33,Jogging,49105962326000,-0.6946376999999999,12.680544,0.50395286;
1,33,Jogging,49106062271000,5.012288,11.264028,0.95342433;
2,33,Jogging,49106112167000,4.903325,10.882658000000001,-0.08172209;
3,33,Jogging,49106222305000,-0.61291564,18.496431,3.0237172;
Run Code Online (Sandbox Code Playgroud)

可以看出,最后一列以分号结尾,所以当我读入熊猫时,该列被推断为类型对象(以分号结尾。

df = pd.read_csv('data.txt')
df
    user    activity    timestamp   x-axis  y-axis  z-axis
0   33  Jogging     49105962326000  -0.694638   12.680544   0.50395286;
1   33  Jogging     49106062271000  5.012288    11.264028   0.95342433;
2   33  Jogging     49106112167000  4.903325    10.882658   -0.08172209;
3   33  Jogging     49106222305000  -0.612916   18.496431   3.0237172;
Run Code Online (Sandbox Code Playgroud)

我如何让熊猫忽略那个分号?

python csv pandas

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

指向非常量数据的常量指针

考虑以下情况1:

const int n = 5;
int* p = &n; 
Run Code Online (Sandbox Code Playgroud)

这是无效的,因为&nis 的类型cont int*pis 的类型int *(类型不匹配错误)。

现在,考虑这种情况 2:

int k = 4;
int *const p = &k; 
Run Code Online (Sandbox Code Playgroud)

本例编译成功,没有任何错误。显然,p是 类型int * const并且&k是 类型int *。在这种情况下,存在类型不匹配,但它是有效的。

问题:即使存在类型不匹配,为什么第二种情况有效?

c++ pointers constants type-mismatch

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

是否必须为字符数组的初始化提供 '\0'?

请看代码:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char name[10] = {'L','e','s','s','o','n','s'}; // did not add `\0`
    char c;

    for (int i = 0; name[i] != '\0'; i++)
    {
        c = name[i];
        printf("%c", c);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我最后没有给\0,但仍然有效。

我的问题:\0在上面的情况下是自动添加的吗?

编辑:在这些情况下会发生什么:

  1. char name[7] = {'L','e','s','s','o','n','s'};
  2. char name[8] = {'L','e','s','s','o','n','s'};

我很抱歉编辑问题。

c arrays c-strings

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

当其他成员设置为新值时,C union成员给出特定/错误值.为什么此输出在C中的以下代码中?

#include <stdio.h>
int main()
{
  union Data
  {
    char str[20];    
    int i;
    float f;
  }data;

  data.i=20;
  data.f=220.5;
  printf("%d\n",(data.i));

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出为:1130135552.我在Ubuntu 16.04 LTS上使用了gcc complier.

有人可以解释输出吗?

成员data.idata.f占用相同的内存位置,所以输出应该是220.但为什么输出是1130135552?

c unions

3
推荐指数
2
解决办法
180
查看次数

为什么销毁顺序与构造顺序相同,使用静态对象(C++)?

代码是:

#include <iostream>
using namespace std;

class A
{
public:
    A() { cout << "A::A" << endl; }
    ~A() { cout << "A::~" << endl; }
};

class B
{
public:
    B() { cout << "B::B" << endl; }
    ~B() { cout << "B::~" << endl; }
};

int main()
{
    B b;
    static A a;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

B::B
A::A
B::~
A::~
Run Code Online (Sandbox Code Playgroud)

非静态对象b的作用域和静态对象的作用域amain()函数结束时结束。

问题:为什么构造函数的顺序和析构函数的顺序一样?

c++ static constructor destructor

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

为什么 Helgrind 显示“违反锁定顺序”错误消息?

请看下面的代码

    #include <stdio.h>
    #include <pthread.h>
    #include <assert.h>
    #include <stdlib.h>

    pthread_mutex_t g = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;

    void* worker(void* arg) 
    {
        pthread_mutex_lock(&g);

        if ((long long) arg == 0) {
        pthread_mutex_lock(&m1);
        pthread_mutex_lock(&m2);
        } else {
        pthread_mutex_lock(&m2);
        pthread_mutex_lock(&m1);
        }
        pthread_mutex_unlock(&m1);
        pthread_mutex_unlock(&m2);

        pthread_mutex_unlock(&g);
        return NULL;
    }

    int main(int argc, char *argv[]) {
        pthread_t p1, p2;
        pthread_create(&p1, NULL, worker, (void *) (long long) 0);
        pthread_create(&p2, NULL, worker, (void *) (long long) 1);
        pthread_join(p1, NULL);
        pthread_join(p2, NULL);
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

Helgrind …

c multithreading valgrind deadlock pthreads

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

当我们为类的对象分配整数值时,为什么会调用参数化构造函数?

代码是:

#include<iostream>
using namespace std;

class Integer
{
    int num;

    public:
        Integer()
        {
            num = 0;
            cout<<"1";
        }
        
        Integer(int arg)
        {
            cout<<"2";
            num = arg;
        }
        int getValue()
        {
            cout<<"3";
            return num;
        }

};

int main()
{
    Integer i;
    i = 10;  // calls parameterized constructor why??
    cout<<i.getValue();
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,语句i=10调用了参数化构造函数。你能解释一下吗。

c++ constructor assignment-operator parameterized-constructor

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