明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2577|回复: 14

文字串列的問題

  [复制链接]
发表于 2004-6-10 16:41:00 | 显示全部楼层 |阅读模式
请问如何得到下列的结果


'("B" "B0" "B1" "G1" "G2" "G11" "WG1" "WG2" "b1" "b2" "bb1" "bb2" "wg1" "wg2")


-> '("B" "G" "WG" "b" "bb" "wg")                不用考虑排序的问题,如何只取得英文的字头
"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2004-6-10 17:23:00 | 显示全部楼层
(get '("B" "B0" "B1" "G1" "G2" "G11" "WG1" "WG2" "b1" "b2" "bb1" "bb2"
"wg1" "wg2"))
  1. (defun get(lst / item lst2)
  2.    (setq lst (mapcar '(lambda(e) (while (wcmatch e "*[0-9]") (setq e (substr e 1 (1- (strlen e))))) e)
  3.            lst))
  4.    (foreach item lst
  5.        (if (not (member item lst2))
  6.            (setq lst2 (append lst2 (list item)))
  7.        )
  8.    )
  9.    lst2
  10. )
 楼主| 发表于 2004-6-10 17:53:00 | 显示全部楼层
谢谢
 楼主| 发表于 2004-6-11 08:38:00 | 显示全部楼层
感谢meflying的开头,这是处理带英文的数字排序问题,提供分享
  1. (defun get (lst / item)
  2.    (setq lst (mapcar '(lambda(e)
  3.                                (while (wcmatch e "*[0-9]")
  4.                                              (setq e (substr e 1 (1- (strlen e))))
  5.                                )
  6.                                        e)
  7.                  lst)
  8.    )
  9.    (foreach item lst
  10.        (if (not (member item lst2))
  11.            (setq lst2 (append lst2 (list item)))
  12.        )
  13.    )
  14. )(defun c:tttsort()
  15.   
  16.    (setvar "dimzin" 8)
  17.   
  18.    (setq lst '("ba2" "ba11" "G1" "G1" "G2" "ba1" "B1" "B2" "b1" "b2" "b11" "b12" "B11" "B12"
  19.                            "g11" "b" "B" "g12" "g2" "g1" "b" "wg11" "B" "G1" "B3" "b3" "b0" "B0"
  20.                            "WG1" "WG11" "WG2" "wg1" "wg11" "wg2" "B101" "B111" "B99" "b1" "G111"
  21.                            "g22" "g101" "g110" "b99" "G33"))   (setq lst (vl-sort lst '<))
  22. (print lst)
  23.   
  24.    (get lst)   (setq lst2 (vl-sort lst2 '<))   (setq tsort1 '())
  25.    (setq i 0)
  26.    (repeat (length lst2)
  27.          (setq txt (nth i lst2))
  28.          (setq j 0)
  29.          (setq tsort '())
  30.          (repeat (length lst)
  31.                (setq txtt (nth j lst))
  32.                (setq txtlen (strlen txt))
  33.                (setq mark2 (ascii (substr txtt (+ txtlen 1) 1)))
  34.                (setq txtnx (substr txtt (+ txtlen 1)))
  35.                (if (= txtnx "")(setq txtnx "-0.5"))
  36.                (if (or (and (>= mark2 65)(<= mark2 90))(and (>= mark2 97)(<= mark2 122)))
  37.                        (setq mark2 T)
  38.                )
  39.                (setq mark1 (wcmatch txtt (strcat txt "*")))
  40.                (if (and (= mark1 T)(/= mark2 T)) (setq tsort (cons txtnx tsort)))
  41.                (setq j (+ j 1))
  42.            )           (setq tsort (mapcar 'atof tsort))            
  43.            (setq txtnx (vl-sort tsort '<))
  44.   
  45.              (setq tsort3 '())
  46.              (setq k 0)
  47.              (repeat (length txtnx)
  48.                    (setq txk (nth k txtnx))
  49.                    (setq txkk (strcat txt (rtos txk)))
  50.                    (if (= txk -0.5)(setq txkk txt))
  51.                    (setq tsort3 (cons txkk tsort3))
  52.                    (setq k (+ k 1))
  53.              )              (setq tsort1 (append tsort3 tsort1))           (setq i (+ i 1))
  54.   
  55.      )
  56.      (setq tsort1 (reverse tsort1))
  57.      (print tsort1)
  58. (prin1)
  59. )
  60.   
发表于 2004-6-11 08:56:00 | 显示全部楼层
我发现很多人的编程都不习惯用局部变量,我觉得这样不好,,,


如上面的,LST2要作为GET的局部变量,在主函数中这样调用


(setq lst2 (get lst))


这样,不需要知道GET函数的内部情况,如:我们不需要他里面用了变量LST2,只需调用它取得返回值就可以了


而且,这样后,GET函数可以在别的地方调用,如:


(setq bbb (get lstx));如果按你原来的方法,要把bbb变为排序后的表,就得再写一个类似的GET函数
 楼主| 发表于 2004-6-11 09:05:00 | 显示全部楼层
meflying发表于2004-6-11 8:56:00我发现很多人的编程都不习惯用局部变量,我觉得这样不好,,,



如上面的,LST2要作为GET的局部变量,在主函数中这样调用


(setq lst2 (get lst))


这样,...

这是很好的观念,谢谢提点
发表于 2004-6-11 09:32:00 | 显示全部楼层
带英文的数字排序


好像写的长了些,呵呵,说说而以,别介意
 楼主| 发表于 2004-6-11 09:34:00 | 显示全部楼层
无痕发表于2004-6-11 9:32:00带英文的数字排序 好像写的长了些,呵呵,说说而以,别介意

哈哈,就等你来个精简代码
发表于 2004-6-16 00:40:00 | 显示全部楼层
终于考完试了....
  1. ;含数字的字符串排序-----原创 by 无痕.2004.6(defun strbrk (str / strl s1 s2)
  2.    (setq strl (vl-string->list str))
  3.    (mapcar '(lambda(x)(if (<= 48 x 57) (setq s2 (cons x s2)) (setq s1 (cons x s1)))) strl)
  4.    (list (reverse s1) (reverse s2))
  5. )
  6. (defun strsort (strlst / lst i)
  7.    (setq lst   (mapcar 'strbrk strlst)
  8.   lst (vl-sort lst '(lambda (x y) (< (read (vl-list->string(cadr x)))(read (vl-list->string (cadr y))))))
  9.   i       (apply 'max (mapcar '(lambda(x)(length(car x))) lst)))
  10.    (repeat i
  11.        (setq i       (1- i)
  12.      lst (vl-sort lst '(lambda (x y) (< (nth i (car x)) (nth i (car y))))))
  13.    )
  14.    (mapcar '(lambda(x)(strcat (vl-list->string (car x))(vl-list->string (cadr x)))) lst)
  15. )
  1. ;|
  2. 已知:
  3. (setq  strlst '("ba2"     "ba11"   "G1"       "G1"       "G2"       "ba1"     "B1"
  4.              "B2"       "b1"       "b2"       "b11"     "b12"     "B11"     "B12"
  5.              "g11"     "b"         "B"         "g12"     "g2"       "g1"       "b"
  6.              "wg11"   "B"         "G1"       "B3"       "b3"       "b0"       "B0"
  7.              "WG1"     "WG11"   "WG2"     "wg1"     "wg11"   "wg2"     "B101"
  8.              "B111"   "B99"     "b1"       "G111"   "g22"     "g101"   "g110"
  9.              "b99"     "G33"
  10.            )
  11. )
  12. 测试:(strsort strlst) ->
  13. ("B" "B" "B0" "B1" "B2" "B3" "B11" "B12" "B99" "B101" "B111" "G1" "G1" "G1"
  14. "G2" "G33" "G111" "WG1" "WG2" "WG11" "b" "b" "b0" "b1" "b1" "b2" "b3" "b11"
  15. "b12" "b99" "ba1" "ba2" "ba11" "g1" "g2" "g11" "g12" "g22" "g101" "g110" "wg1"
  16. "wg2" "wg11" "wg11")
  17. |;
发表于 2004-6-27 19:09:00 | 显示全部楼层
  1. (defun str-sort(lst / func)
  2.   (defun func(str / num s str1)
  3.    (setq s"" str1 str)
  4.    (while(and(/= str1 "")(null(setq num(distof str1))))
  5.            (setq s(strcat s (substr str1 1 1)) str1 (substr str1 2) )
  6.    )
  7.    (list s (if num num -1e-20) str)
  8.   )
  9.   ;;;
  10.   (mapcar 'last
  11.    (vl-sort (mapcar 'func lst)
  12.        '(lambda(x y / x1 y1)(setq x1(car x) y1(car y))
  13.                (if(= x1 y1)(<(cadr x)(cadr y))(< x1 y1))
  14.        )
  15.    )
  16.   )
  17. )
(str-sort
'("w45""w-3""s""z1""z-4""df676""df24""bff4""bff""bff2""A34""A""A2""A5""bff-5"))-->
("A" "A2" "A5" "A34" "bff-5" "bff" "bff2" "bff4" "df24" "df676" "s" "w-3" "w45"
"z-4" "z1")
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-10-1 02:25 , Processed in 0.188796 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表