取数组中每3点的平均值并将新值保存到新数组中

时间:2019-03-15 12:57:45

标签: python arrays python-3.x loops for-loop

我试图取数组中每3点的平均值,并将新值输出到新数组中。但是,由于我不断收到相同的错误消息,因此代码中的某些内容一定是错误的。

这是我的代码:

wchar_t* mb2wstr(const char* inval) {
    size_t size = std::strlen(inval);
    #define OUTSZ (size+1)*sizeof(wchar_t)
    auto buf = (wchar_t*)std::malloc(OUTSZ);
    std::memset(buf, 0, OUTSZ);
    std::setlocale(LC_CTYPE,""); //  необходима, чтобы отработала "mbstowcs"
    size = std::mbstowcs(buf, inval, size);
    if ( size == (size_t)(-1) ) {
        std::free(buf);
        buf = nullptr;
    } else {
        buf = (wchar_t*)std::realloc(buf,OUTSZ);
    }
    return buf;
    #undef OUTSZ
}

char* wstr2mb(const wchar_t* inval) {
    size_t size = std::wcslen(inval);
    #define OUTSZ (size+1)*MB_CUR_MAX // Maximum length of a multibyte character in the current locale
    auto buf = (char*)std::malloc(OUTSZ);
    std::memset(buf, 0, OUTSZ);
    std::setlocale(LC_CTYPE,""); //  необходима, чтобы отработала "wcstombs"
    size = std::wcstombs(buf, inval, size*sizeof(wchar_t));
    if ( size == (size_t)(-1) ) {
        std::free(buf);
        buf = nullptr;
    } else {
        buf = (char*)std::realloc(buf,size+1);
    }
    return buf;
    #undef OUTSZ
}

const std::string pwchar2string(const wchar_t* inval) {
    char* tmp = wstr2mb(inval);
    string out{tmp};
    std::free(tmp);
    return out;
}
const std::wstring pchar2wstring(const char* inval) {
    wchar_t* tmp = mb2wstr(inval);
    wstring out{tmp};
    std::free(tmp);
    return out;
}

const wstring string2wstring(const string& value) {
    return pchar2wstring(value.c_str());
}
const string wstring2string(const wstring& value) {
    return pwchar2string(value.c_str());
}
const wchar_t* char2wchar(const char* value) {
    return pchar2wstring(value).c_str();
}
const char* wchar2char(const wchar_t* value) {
    return pwchar2string(value).c_str();
}

错误消息:

# y average values

avgy = [0]*(len(y1)//3)

for i in range (0, len(y1)-2):
    if (3*(1+i)<=len(y1)):
        avgy[i] = ( y1[3*i+1] + y1[3*i+2] y1[3*i])/3


# x average values

avgx = [0]*(len(x1)//3)

for i in range (0, len(x1)-2):
    if (3*(1+i)<=len(x1)):
        avgx[i] = ( x1[3*i+1] + x1[3*i+2] x1[3*i])/3

预先感谢, 奥利弗

1 个答案:

答案 0 :(得分:2)

在y1 [3 * i])/ 3和x1 [3 * i])/ 3之前缺少+

# y average values

avgy = [0]*(len(y1)//3)

for i in range (0, len(y1)-2):
    if (3*(1+i)<=len(y1)):
        avgy[i] = ( y1[3*i+1] + y1[3*i+2] + y1[3*i])/3


# x average values

avgx = [0]*(len(x1)//3)

for i in range (0, len(x1)-2):
    if (3*(1+i)<=len(x1)):
        avgx[i] = ( x1[3*i+1] + x1[3*i+2] + x1[3*i])/3