搜索
人大经济论坛 标签 字符串 相关日志

tag 标签: 字符串经管大学堂:名校名师名课

相关日志

分享 hive_数组字段转成字符串
xulimei1986 2017-4-7 14:06
2. 表test_array有数组字段例如: select c_array from test_array where dt='2016-09-26' and size(c_array)=2 limit 2; 3.转为字符串: select concat_ws(',',c_array) fromtest_arraywhere dt='2016-09-26' and size(c_array)=2 limit 2; 105750,1246345,907964 935105,359772,935104
0 个评论
分享 Matlab学习系列006:字符串
zhjx19 2015-1-17 19:14
Matlab学习系列006:字符串
006. 字符串 字母、数字、特殊符号都是字符;一串连续的字符就是字符串。 一般认为字符串是由空格和多个字符构成,记为 str = ’a1 a2 …an’ (n ≥ 0) ai 可以是字母、数字、特殊符号、空格, 每个字符占 1 位存放成一个行向量( 1 × n 矩阵),从而可以通过下标访问字符串的元素。 多个字符串也可以构成字符矩阵,但必须长度相同。 一、 字符串的创建 1. 将字符串的字符放在一组英文单引号中间即可,例如 str1 = 'We''re going to study Matlab!' % 必须英文状态下的单引号,单引号元素用两个单引号 l = length(str1) % l 返回列数,即字符串包含字符的个数 zhstr1 = ' 中文字符串示例! ' % 中文字符串,也是英文单引号 size (zhstr1) 运行结果: str1 = We're going to study Matlab! l = 28 zhstr1 = 中文字符串示例! ans = 1 8 2. 用元胞数组存放复杂字符串,或 cellstr() 函数 C1 = {'Matlab 2010b includes data types:'; 'Double array'; 'Character array'; 'and so on'} class(C1) % 返回 C1 的数据类型 size(C1) C2 = char('Matlab 2010b includes data types:', ... 'Double array', ... 'Character array',... 'and so on') class(C2) size(C2) C3 = cellstr(C2); % 同 C1 运行结果: C1 = 'Matlab 2010b includes data types:' 'Double array' 'Character array' 'and so on' ans = cell ans = 4 1 C2 = Matlab 2010b includes data types: Double array Character array and so on ans = char ans = 4 33 注:这里 C1 也可以用 strvcat(str1, str2,…) 实现类似的存储效果,见下文【字符串的连接】。 二、 字符串的访问和操作 字符串是以行向量形式存储的,可通过下标访问。 1. 替换字符串中的元素 str1 = 'We''re going to study Matlab!'; str1(16:20) = 'learn' % 将 study 替换为 learn ,注意 study 是从第 16 个位置开始的 运行结果: str1 = We're going to learn Matlab! 2. 取出字符串的子串 str1 = 'We''re going to study Matlab!'; str2 = str1(16:20) 运行结果: str2 = learn 3. 字符串顺序的倒排 str1 = 'We''re going to study Matlab!'; str3 = str1(end:-1:1) 运行结果: str3 = !baltaM nrael ot gniog er'eW 4. 字符串字符的 ASCII 值与字符相互转换 字符串的元素存放的是字符的 ASCII 码值,显示在屏幕上的是字符本身。 从字符到 ASCII 码: double( ) 从 ASCII 码到字符: char( ) str1 = 'We''re going to study Matlab!'; ustr1 = double(s1) str4 = char(us1) zhstr1 = ' 中文字符串示例! '; double(zhstr1) 运行结果: ustr1 = Columns 1 through 15 87 101 39 114 101 32 103 111 105 110 103 32 116 111 32 Columns 16 through 28 108 101 97 114 110 32 77 97 116 108 97 98 33 str4 = We're going to learn Matlab! ans = 20013 25991 23383 31526 20018 31034 20363 65281 5. 字符串英文字母转换大小写 str1 = 'We''re going to study Matlab!'; upper(str1) % 全变为大写 lower(str1) % 全变为小写 运行结果: ans = WE'RE GOING TO LEARN MATLAB! ans = we're going to learn matlab! 6. 字符串的连接 strcat(str1, str2,…) ——将字符串 str1, str2,… 连接合并为一个长字符串; strvcat(str1, str2,…) ——将字符串 str1, str2,… 连接成字符串向量( n × 1 的字符串 矩阵,或 n × mi 字符矩阵); str1 = 'abcdefg'; str2 = 'hijklmnopq'; str3 = strcat(str1,str2) % 将两个字符串合并为一个长字符串 str4 = strvcat(str1,str2) % 将两个字符串连接成字符串向量 whos str3 % 返回 str3 的结构形式 whos str4 % 2 x 1 的字符串矩阵 运行结果: str3 = abcdefghijklmnopq str4 = abcdefg hijklmnopq Name Size Bytes Class Attributes str3 1x14 28 char Name Size Bytes Class Attributes str4 2x7 28 char 7. 字符串的比较 strcmp(str1,str2) —— str1 与 str2 相等返回 1 ,否则返回 0 (区分大小写); strcmpi(str1,str2) —— str1 与 str2 相等返回 1 ,否则返回 0 (不区分大小写); strncmp(str1,str2,n) ——比较 str1 与 str2 前 n 个字符是否相等(区分大小写) strncmpi(str1,str2,n) ——比较 str1 与 str2 前 n 个字符是否相等(不区分大小写) strcmp(str1,str2) strcmpi('abCD','abcd') strncmp('Abcde',str3,5) strncmpi('Abcde',str3,5) 运行结果: ans = 0 ans = 1 ans = 0 ans = 1 8. 字符串的查找与替换 findstr(str1,str2) ——在较长字符串中查找较短字符串出现的各个位置( str1 , str2 谁先谁后都没关系); strfind(str1,key) ——在字符串 str1 中查找字符串 key 出现的位置; strmatch(key,strs) ——检查多行的字符串 strs ,以列向量形式返回各行以字符串 key 开始的各个行号; strrep(str1,str2,str3) ——把 str1 中含有 str2 位置用 str3 替换; str = 'Find the starting indices of the shorter string.'; findstr(str, 'the') findstr('the', str) strfind(str,'the') strfind('the',str) strmatch('hij',str4) % 字符串 str4 第 2 行是以 ’hij’ 开始的 strrep(str,'the','a') 运行结果: ans = 6 30 ans = 6 30 ans = 6 30 ans = ”“;”等都保留; int2str(A) ——整数矩阵转换为字符串向量(字符矩阵)。 A = whos A str = num2str(A) whos str num = str2num(str); % num 等于 A str1 = mat2str(A) whos str1 str2 = int2str(A); % 其值等于前面的 str , % 若 A 不是整数矩阵先四舍五入为整数矩阵 运行结果: A = 1 2 3 4 5 6 77 88 99 Name Size Bytes Class Attributes A 3x3 72 double str = 1 2 3 4 5 6 77 88 99 Name Size Bytes Class Attributes str 3x10 60 char str1 = Name Size Bytes Class Attributes str1 1x22 44 char 10. 其他函数 blanks(n) ——返回 n 个空格组成的字符串; deblank(str) ——删除字符串末尾的空格; strtrim(str) ——删除字符串开头、结尾的空格、制表符、换行符; isspace(str) ——返回和 str 同样大小的向量,空格、制表符、换行符的位置是 1 ,其他位置是 0 ; lasterr ——返回上一个错误信息的字符串。 三、 字符串的应用 1. eval() 函数——将括号内的字符串视为执行代码并运行 例如, eval('y=sin(pi/2)') 和 y = sin(pi/2) 等价 运行结果都是: y = 1 Eval() 函数多在循环中搭配 num2str() 函数一起使用,可以对多个名字有规则的变量或文件进行操作,例如, for k=1:3; eval( ) % 实现 yk = k^2 end D = {'odedemo'; 'sunspots'; 'fitdemo'}; % 三个 m 文件名 n = input('Select a demo number: '); % 提示输入要选择执行的某个 m 文件,比如输入 2 eval(D{n}) % 运行 sunspots.m 运行结果: y1 = 1 y2 = 4 y3 = 9 运行 Matlab 自带的 sunspots.m 文件,输出结果是一个图(略) 2. 用字符串命令控制格式化输出 例, fprintf , sprintf , sscanf 用法 a = rand(2,2) % 生成 2 × 2 随机矩阵 ss = sprintf('%.10e\n',a) % 按 10 位科学计数法输出, % 每输一个换行,注意: ss 是字符矩阵 fprintf('%.4g\n',a) % 按 4 位数输出 sscanf(ss, '%f', ) % 按浮点格式转换成 2 × 2 数值矩阵 运行结果: a = 0.8147 0.1270 0.9058 0.9134 ss = 8.1472368639e-001 9.0579193708e-001 1.2698681629e-001 9.1337585614e-001 ans = 0.8147 0.9058 0.127 0.9134 ans = 0.8147 0.1270 0.9058 0.9134 3. 用字符串给图形标记文字说明 a=2; w=3; t=0:0.01:10; y=exp(-a*t).*sin(w*t); =max(y); % 找出 y 的最大值元素位置 t_text= ; % 生成最大值点横坐标字符串 y_text= ; % 生成最大值点纵坐标字符串 max_text=char('maximum',t_text,y_text); tit= ; % 生成标记最大值点的字符串 plot(t,zeros(size(t)),'k') % 画纵坐标为 0 的基准线 hold on % 保持已绘制图形不被清除 plot(t,y,'b') % 用蓝色线画函数 y(t) plot(t(i_max),y_max,'r.','MarkerSize',20) % 用大红点标记最大值点 text(t(i_max)+0.3,y_max+0.05,max_text) % 显示最大值点的数值 title(tit) % 标记图名 xlabel('t') % 标记横坐标名 ylabel('y') % 标记纵坐标名 hold off % 清除原来的图 运行结果:
个人分类: Matlab学习系列|41 次阅读|0 个评论
分享 处理中文字符串的好帖子
xulimei1986 2014-9-19 19:28
https://bbs.pinggu.org/thread-878037-1-1.html
0 个评论
分享 Numpy教程
Nicolle 2014-8-7 03:10
Numpy教程 来源: http://reverland.org/python/2012/08/22/numpy/ 目录 先决条件 基础篇 一个例子 创建数组 打印数组 基本运算 通用函数(ufunc) 索引,切片和迭代 形状操作 更改数组的形状 组合(stack)不同的数组 将一个数组分割(split)成几个小数组 复制和视图 完全不拷贝 视图(view)和浅复制 深复制 函数和方法(method)总览 进阶 广播法则(rule) 花哨的索引和索引技巧 通过数组索引 通过布尔数组索引 ix_()函数 用字符串索引 线性代数 简单数组运算 矩阵类 索引:比较矩阵和二维数组 技巧和提示 “自动”改变形状 向量组合(stacking) 直方图(histogram) 参考文献
个人分类: Python|9 次阅读|0 个评论
分享 字符串长度定义
yukai08008 2013-2-28 10:19
如果在之前的数据集被定义长度,则长度固定;如非则‘=’可以对字符串长度定义(可能超过8个字符{默认长度})
个人分类: 学习笔记|0 个评论
GMT+8, 2025-12-24 21:52