描述:在 C++ 中构建用于标记字符串(包括空格)的基本代码。
主要思想:调用一个本地函数,该函数使用空值作为开头来计算字符串中的所有空格。
问题:在到达第二个令牌之前提前终止。
#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;
char *next_token = NULL;
int count_spaces(const char *p);
char *p;
int main() {
char s[81];
cout << "Input a string : ";
cin.getline(s, 81);
p = strtok_s(s, ", ", &next_token);
while (p != nullptr) {
cout << p << endl;
p = strtok_s(nullptr, ", ", &next_token);
}
count_spaces(p);
cout << count_spaces(p);
return 0;
}
int count_spaces(const char *p) {
int number = 0, …Run Code Online (Sandbox Code Playgroud) 我想对字符串进行标记化,直到某个分隔符出现第三次为止,然后将字符串的其余部分作为标记化数组的最后一个元素返回。示例:我有一个如下所示的字符串:
String someString= 1.22.33.4
Run Code Online (Sandbox Code Playgroud)
现在我通过分隔符“.”对其进行标记。像这样:
def (a, b, c, d) = someString.tokenize('.')
Run Code Online (Sandbox Code Playgroud)
它有效,但前提是点数恰好是 3。现在,如果有人输入更多点数,例如:
String someString = 1.22.33.4.55
Run Code Online (Sandbox Code Playgroud)
那么它就不起作用,因为变量的数量不匹配。所以我想确保它只标记最多第三个点,然后返回剩下的内容。所以在这种情况下我想要实现的是:
a = 1, b=22, c=33, d=4.55
Run Code Online (Sandbox Code Playgroud)
怎么做?
我在 stackoverflow 上读到的类似错误的所有答案都建议修复空值或修复数据类型。我的数据帧或浮点数中都没有空值。但是,错误仍然存在。
以下是有关我的数据的一些信息:
关于空值(据我所知,numpy.nans 在 pandas 中被编码为浮点数):

关于数据类型:

当我这样做时:
from tensorflow.keras.preprocessing.text import Tokenizer
title_tokeniser = Tokenizer(num_words=10)
title_tokeniser.fit_on_texts(train_set.loc[:,'title'] + test_set.loc[:,'title'])
Run Code Online (Sandbox Code Playgroud)
这是错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-38-26b704f1c0a1> in <module>()
1 title_tokeniser = Tokenizer(num_words=10)
----> 2 title_tokeniser.fit_on_texts(train_set.loc[:,'title'] + test_set.loc[:,'title'])
3
4 # unique tokens found in titles are:
5 title_token_index = title_tokeniser.word_index
1 frames
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/text.py in fit_on_texts(self, texts)
223 self.filters,
224 self.lower,
--> 225 self.split)
226 for w in seq:
227 if w in self.word_counts:
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/text.py in …Run Code Online (Sandbox Code Playgroud) 我如何从用户那里获得一个数字列表,然后将它们标记化.
这就是我所拥有的,但除了第一个数字之外它没有得到任何东西:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string line = "";
cin >> line;
stringstream lineStream(line);
int i;
vector<int> values;
while (lineStream >> i)
values.push_back(i);
for(int i=0; i<values.size(); i++)
cout << values[i] << endl;
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想将字符串拆分为python中的列表,具体取决于数字/非数字.例如,
5 55+6+ 5/
Run Code Online (Sandbox Code Playgroud)
应该回来
['5','55','+','6','+','5','/']
Run Code Online (Sandbox Code Playgroud)
我现在有一些代码循环遍历字符串中的字符并使用re.match("\ d")或("\ D")测试它们.我想知道是否有更好的方法来做到这一点.
PS:必须与python 2.4兼容
我需要一个非常快的字符串拆分函数,它会拆分逗号分隔的字符串,而不会拆分包含逗号的双引号中的字符串.有没有这样做的功能?如果最好通过正则表达式处理,请指出必要的模式,如果适用,请告诉我任何我应该了解的速度优化提示.例如,如果有一种方法以这样的方式调用正则表达式,即不需要每次都重新评估正则表达式模式等.此函数将在短时间内被调用数千次.
注意,我确实在SO上看到了正则表达式帖子,如下所示:
但它们是C#和其他语言,而不是Java.此外,如果有一个非正则表达式方法更快,我想知道它,如上所述.
- roschler
我正在逐行读取CSV并标记每个逗号分隔值.每个标记都是字符串类型.我把它放入float类型的向量中.在下面的例子中,例如,如果csv中的值是"0.08",*beg ="0.08",但是在向量v中它是"0.079999998"
有没有我可以将矢量的精度设置为3位小数或其他东西.
例:
string line;
boost::char_separator<char> sep(",");
typedef boost::tokenizer< boost::char_separator<char> > t_tokenizer;
ifstream myfile (fileName);
if(myfile.is_open())
{
while (myfile.good())
{
getline (myfile,line);
t_tokenizer tok(line, sep);
for (t_tokenizer::iterator beg = tok.begin(); beg != tok.end(); ++beg)
{
string temp = *beg;
this->v.push_back(::atof(temp.c_str()));
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个包含此内容的二进制文件:
abc\0def\0ghi\0
Run Code Online (Sandbox Code Playgroud)
内容已经被读取并存储在字符串变量s中.我应该如何将组件"abc","def","ghi"提取到不同的字符串标记中?split,stringTokenizer等常用方法不接受\0分隔符.
这个界面后面的东西:
class StreamTokenizer
{
public:
StreamTokenizer(const std::string delimiter);
std::list<std::string> add_data(const std::string);
std::string get_left_over();
};
StreamTokenizer d(" ");
std::list<std::string> tokens;
tokens.append(d.add_data("tok"));
tokens.append(d.add_data("1 t"));
tokens.append(d.add_data("ok2 tok3"));
tokens.push_back(d.get_left_over());
// tokens = {tok1, tok2, tok3}
// d = {}
Run Code Online (Sandbox Code Playgroud)
它以块的形式接收数据,它应该返回它到目前为止找到的所有标记,它应该能够将剩余部分连接到下一个块,并且它不应该保存已经被标记化的数据.
请不要建议使用stringstream,除非你可以展示如何从中删除已经标记化的数据(我的流几乎是无限的)
我最近开始搞乱javascript并偶然发现了一些问题.
我允许我的用户将一系列由空格分隔的数字插入到文本字段中.我试图从文本字段中读取字符串并将数字存储在数组中.但是,我不情愿地添加了0.我一遍又一遍地通过我的代码,但我找不到什么错.
代码:
function get_input(str)
{
var arr = [];
var elem=0;
for(var i=0,j=1; i<str.length ;i++,j++)
{
if (j == str.length) {elem += str[i];
arr.push(elem);
return arr;}
else if (str[j]== " ")
{
elem *=10;
elem +=str[i];
arr.push(elem);
elem=0;
i++;
j++
}
else
{
elem *=10;
elem += str[i];
}
}
return arr;
}
Run Code Online (Sandbox Code Playgroud)
例如输入:123 45 6输出为:10203*405*06*
将非常感谢帮助.
tokenize ×10
c++ ×4
string ×3
java ×2
python ×2
regex ×2
split ×2
atof ×1
boost ×1
character ×1
escaping ×1
groovy ×1
javascript ×1
keras ×1
performance ×1
stringstream ×1
tensorflow ×1
text ×1
vector ×1