Append List and Simplify

Jesús Padilla

Usenet Archive
Joined
Aug 18, 2003
Messages
29
Reaction score
0
I have the next list:
'('("Table" . 45) '("Door" . 28) '("Star" . 56) '("Block" . 45) '("Floor" .
28) '("Wall" . 56))

and I need the next
'('(45 . "Table" "Block") '(28 . "Door" "Floor") '(56 . "Wall" "Star"))

In the fisr list I have many assoc. Example: '("String" . Integer)

In the second list I want to make new associations using the integer on the first element and append all the string that are associated with the same integer on the first list. Example: '(Integer . "String1" "String2")

I have tried APPEND and CONS but it doesn´t work

I Will Appreciate any help about.

JPadilla
 
First what's it for?

Next (cons 8 (list "it" "will" "work" "like" "this"))

If you're planning on xdata, structure it by "," separation or individual components.
 
This should help.

(setq alst (list '("Table" . 45) '("Door" . 28) '("Star" . 56) '("Block" . 45))) (setq keys (vl-sort (mapcar 'cdr alst) '<)) (foreach x keys (setq nlst (cons (cons x (kca_massoc x alst)) nlst))
)

(defun kca_massoc (key alist / x nlist)
(foreach x alist
(if (eq key (cdr x))
(setq nlist (cons (car x) nlist))
)
)
(reverse nlist)
)
 
Do you mean ...

Code:
(defun CollectCdrs ( lst / result item key )

(foreach pair

(mapcar
'(lambda (pair)
(cons
(cdr pair)
(car pair)
)
)
lst
)

(setq result
(if
(setq item
(assoc
(setq key (car pair))
result
)
)
(subst
(cons
key
(list
(cdr item)
(cdr pair)
)
)
item
result
)
(cons
pair
result
)
)
)
)

(reverse result)

)

(CollectCdrs
'(  ("Table" . 45)
("Door" . 28)
("Star" . 56)
("Block" . 45)
("Floor" . 28)
("Wall" . 56)
)
)

Returns ...

(  (45 "Table" "Block")
(28 "Door" "Floor")
(56 "Star" "Wall")
)

While not in the dotted format you expressly identified, perhaps you can modify to suit.

Cheers,

Michael.

I have the next list:
'('("Table" . 45) '("Door" . 28) '("Star" . 56) '("Block" . 45) '("Floor" .
28) '("Wall" . 56))

and I need the next
'('(45 . "Table" "Block") '(28 . "Door" "Floor") '(56 . "Wall" "Star"))

In the fisr list I have many assoc. Example: '("String" . Integer)

In the second list I want to make new associations using the integer on the first element and append all the string that are associated with the same integer on the first list. Example: '(Integer . "String1" "String2")

I have tried APPEND and CONS but it doesn´t work

I Will Appreciate any help about.

JPadilla
 
Another one just for fun:
(setq a (list '("Table" . 45)
'("Door" . 28)
'("Star" . 56)
'("Block" . 45)
'("Floor" . 28)
'("Wall" . 56)
'("Furniture" . 30)))

(defun additem (itm lst)
(if
(atom lst)
(list itm lst)
(cons itm lst)))

(defun reorganize (lst / newlst old)
(foreach item lst
(setq newlst
(if (setq old (assoc (cdr item) newlst))
(subst (cons (car old) (additem (car item) (cdr old))) old newlst) (cons (cons (cdr item) (car item)) newlst))))
newlst)
 
How a bout a singe call:

(setq a (list '("Table" . 45)
'("Door" . 28)
'("Star" . 56)
'("Block" . 45)
'("Floor" . 28)
'("Wall" . 56)
'("Furniture" . 30)))
(setq tmp nil)

(foreach p a
(cond ((assoc (cdr p) tmp)
(setq tmp
(subst (append (assoc (cdr p) tmp) (list (car p)))
(assoc (cdr p) tmp) tmp)))
(T (setq tmp (cons (list (cdr p) (car p)) tmp)))))

(prin1 tmp)

-David
 
Hi David

Your function is great

In this simple problem I learn that there are many ways to get the same goal

Thanks

JPadilla
 

Latest threads

Back
Top