将列表划分为多个列表
大家好,我想写一个小代码,将一个列表划分为多个列表,并在to分隔符之间。
示例:(listToManyLists lst“{”“}”)
(setq f (open (strcat "c:\\1.txt" "r""))
(while (setq x (read-line f)) (setq lst (cons x lst)))
(listToManyLists lst "{" "}")
(defun listToManyLists( lst sep1 sep2 / x y e f)
(if lst
(progn
(setq f t)
(while lst
(setq x (car lst))
(if (= x sep1) (setq f t))
(if (= x sep2) (setq f nil))
(if f (setq y (cons x y)))
(setq lst (cdr lst))
);while
);progn
);if
y
);defun
这是将列表划分为多个列表的最快方法吗? 你能举一个输入数据的例子吗?
文件。。。1.txt。txt文件 也许是这样?
(defun c:test ( / des lst str sub txt )
(if (and (setq txt (findfile "1.txt"))
(setq des (open txt "r"))
)
(progn
(while (setq str (read-line des))
(if (wcmatch str "{,}")
(if sub
(setq lst (cons (reverse sub) lst)
sub nil
)
)
(setq sub (cons str sub))
)
)
(close des)
(if sub (setq lst (cons (reverse sub) lst)))
(print (reverse lst))
)
(princ "\n1.txt not found.")
)
(princ)
)
非常聪明的李。。。(如果(wcmatch str{,}”)。。。
非常感谢。 这可能是将列表划分为多个列表的最快方法。递归不是好方法。
(defun c:test ( / des lst str sub txt goIn )
; created by Lee Mac
; updated by Vlisp
(if (and (setq txt (findfile "c:\\1.txt"))
(setq des (open txt "r"))
)
(progn
(setq lst nil
sub nil
x nil
)
(while (setq str (read-line des))
(if (= str "{") (setq goIn t))
(if goIn (setq x (cons str x)))
(if (= str "}")
(progn
(setq lst (cons (reverse x) lst)
goIn nil
x nil
)
);progn
);if
);while
(close des)
lst
);progn
(princ "\n1.txt not found.")
);if
);defun
输入数据。。。1-upd。txt文件
不客气!
如果愿意,您可以使用递归-下面是编写函数的另一种方法:
(defun c:test ( / des lst str txt )
(if (and (setq txt (findfile "1.txt"))
(setq des (open txt "r"))
)
(progn
(while (setq str (read-line des))
(setq lst (cons str lst))
)
(close des)
(grouplist lst "{" "}")
)
)
)
(defun grouplist ( lst ch1 ch2 / foo itm )
(defun foo ( lst acc )
(cond
( (null lst) (list (reverse acc)))
( (= ch2 (car lst)) (cons (reverse (cons ch2 acc)) (grouplist (cdr lst) ch1 ch2)))
( (foo (cdr lst) (cons (car lst) acc)))
)
)
(if (setq itm (member ch1 lst))
(foo itm nil)
)
) 下面是另一种可能的书写方式:
(defun c:test ( / des lst str sub txt )
(if (and (setq txt (findfile "1.txt"))
(setq des (open txt "r"))
)
(progn
(while (setq str (read-line des))
(if (= "{" str)
(progn
(while (and str (/= "}" str))
(setq sub (cons str sub)
str (read-line des)
)
)
(if str (setq sub (cons str sub)))
(setq lst (cons (reverse sub) lst)
sub nil
)
)
)
)
(close des)
(reverse lst)
)
)
)
谢谢李!
我认为这个递归(grouplist)是比迭代函数(c:test)更快的函数
再次感谢!
页:
[1]
2