首页 > 解决方案 > 忽略:我只是重命名了标题来尝试一下

问题描述

我正在尝试一个一个地在数组中分配一个数字。如果指针指向数组的第一个元素,则表示数组为空,将分配一个数字。但由于某种原因,它似乎没有将指针移动到下一个元素。我怎样才能解决这个问题?

unsigned char number= '1';
unsigned char array[8];
int count = 0;
unsigned char *ptr;



int main(){
    while(count < 5){
        reserve();
        count ++;
        ptr++; //move to next element
    }
}

void reserve(void){
    if(ptr = array){          //if pointer is at the first element of the
        *ptr = number;         //array then it means it's empty
    }
    else{                    //not empty array
        *ptr = number;
    }  
}

标签: c

解决方案


In [1]: from sklearn.feature_extraction.text import CountVectorizer
   ...: 
   ...: text  = ["London Paris London", "Paris Paris London"]
   ...: cv = CountVectorizer()
   ...: count_matrix = cv.fit_transform(text)
   ...: 

结果是scipy.sparse模块中定义的矩阵。它可以以多种方式显示:

In [2]: count_matrix
Out[2]: 
<2x2 sparse matrix of type '<class 'numpy.int64'>'
    with 4 stored elements in Compressed Sparse Row format>
In [3]: print(count_matrix)
  (0, 0)    2
  (0, 1)    1
  (1, 0)    1
  (1, 1)    2
In [4]: count_matrix.toarray()
Out[4]: 
array([[2, 1],
       [1, 2]])
In [5]: count_matrix.A   # shorthand for toarray()
Out[5]: 
array([[2, 1],
       [1, 2]])

在这种情况下,矩阵不是“稀疏”的,即所有值都不为零。但一般来说,当许多值为零时使用这种类型的矩阵。通常我们定义这样一个矩阵

from scipy import sparse
M = sparse.csr_matrix(...)   # see docs

sklearn可以在其某些函数中使用稀疏矩阵输入,并在这种情况下创建一个。有关更多详细信息,请参阅CountVectorizer文档。


推荐阅读