I have a list of lists, where each sublist is labelled by
a number. I need to collect together the contents of all sublists
sharing
the same label. So if I have the list
((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
r) (5 s t))
where the first element of each sublist is the label, I need to
produce:
output:
((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
a Mathematica solution is here:
http://xahlee.org/UnixResource_dir/writ/notations_mma.html
R5RS Scheme lisp solution:
http://xahlee.org/UnixResource_dir/writ/Sourav_Mukherjee_sourav.work_gmail.scm
by Sourav Mukherjee
also, a Common Lisp solution can be found here:
http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/5d1ded8824bc750b?
anyone care to give a solution in Python, Perl, javascript, or other
lang? am guessing the scheme solution can be much improved... perhaps
using some lib but that seems to show scheme is pretty weak if the lib
is non-standard.
Xah ∑ xahlee.org ☄
(mapcar
'((X) (apply conc (cdr X)))
(group List) )
Cheers,
- Alex
Python 3: (I have not tried to match the exact format of your output,
but I get the right things is the right order.)
data = ((0,'a','b'), (1,'c','d'), (2,'e','f'), (3,'g','h'),
(1,'i','j'), (2,'k','l'), (4,'m','n'), (2,'o','p'),
(4,'q','r'), (5,'s','t'))
from collections import OrderedDict
r = OrderedDict()
for label,*rest in data:
r.setdefault(label, []).extend(rest)
print(list(r.values()))
produces:
(['a', 'b'], ['c', 'd', 'i', 'j'], ['e', 'f', 'k', 'l', 'o', 'p'], ['g',
'h'], ['m', 'n', 'q', 'r'], ['s', 't'])
--
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
from collections import defaultdict
def collect(xss):
d = defaultdict(list)
for xs in xss:
d[xs[0]].extend(xs[1:])
return sorted(v for k,v in d.items())
y = [['0','a','b'], ['1','c','d'], ['2','e','f'], ['3','g','h'],
['1','i','j'], ['2','k','l'], ['4','m','n'], ['2','o','p'],
['4','q','r'], ['5','s','t']]
print collect(y)
from collections import defaultdict
def collect(xss):
d = defaultdict(list)
for xs in xss:
d[xs[0]].extend(xs[1:])
return list(v for k,v in sorted(d.items()))
y = [[0,'a','b'], [1,'c','d'], [2,'e','f'], [3,'g','h'], [1,'i','j'],
[2,'k','l'], [4,'m','n'], [2,'o','p'], [4,'q','r'], [5,'s','t']]
print collect(y)
l = [[0, 'a', 'b'], [1, 'c', 'd'], [2, 'e', 'f'], [3, 'g', 'h'], [1,
'i', 'j'], [2, 'k', 'l'], [4, 'm', 'n'], [2, 'o', 'p'], [4, 'q', 'r'],
[5, 's', 't']]
d = {}
for idx, items in [(e[0], e[1:]) for e in l]: d[idx] = d[idx] + items
if idx in d else items
print d.values()
Output:
[['a', 'b'], ['c', 'd', 'i', 'j'], ['e', 'f', 'k', 'l', 'o', 'p'],
['g', 'h'], ['m', 'n', 'q', 'r'], ['s', 't']]
from itertools import groupby
from operator import itemgetter
l = [[0, 'a', 'b'], [1, 'c', 'd'], [2, 'e', 'f'], [3, 'g', 'h'], [1,
'i', 'j'], [2, 'k', 'l'], [4, 'm', 'n'], [2, 'o', 'p'], [4, 'q',
'r'],
[5, 's', 't']]
[
[x for g in gs for x in g[1:]]
for _, gs in groupby(sorted(l), itemgetter(0))
]
--
Arnaud
> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing the same label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
The input is a string on STDIN,
and the output is a string on STDOUT?
Use a hash:
perl -MData::Dumper -wle '$Data::Dumper::Sortkeys = 1;
my $t = "((0 a b) (1 c d) (2 e f) (3 g h) (1 i j)"
. " (2 k l) (4 m n) (2 o p) (4 q r) (5 s t))";
push @{ $h{ $1 } }, $2 while $t =~ /(\w+)([^)]*)/g; # gist
print Dumper \%h;
'
or an array:
perl -wle '
my $t = "((0 a b) (1 c d) (2 e f) (3 g h) (1 i j)"
. " (2 k l) (4 m n) (2 o p) (4 q r) (5 s t))";
push @{$a[$1]},$2 while $t =~ /(\w+)\s+([^)]*)/g; # gist.1
print "((".join(") (",map join(" ",@$_),@a )."))"; # gist.2
'
Or if the list is not just a string, but a real data structure in the
script:
perl -wle'
my $t = [ [qw/0 a b/], [qw/1 c d/], [qw/2 e f/], [qw/3 g h/],
[qw/1 i j/], [qw/2 k l/], [qw/4 m n/], [qw/2 o p/],
[qw/4 q r/], [qw/5 s t/] ];
push @{ $a[ $_->[0] ] }, [ @$_[ 1, 2 ] ] for @$t; # AoAoA
printf "((%s))\n", join ") (",
map join( " ",
map join( " ", @$_ ), @$_
), @a;
'
Etc.
--
Ruud
What the f**** does PicoLisp have to with Perl?
jue
What the f*** does Python have to do with Perl?
jue
> here's a interesting toy list processing problem.
>
> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing
> the same label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>
> a Mathematica solution is here:
> http://xahlee.org/UnixResource_dir/writ/notations_mma.html
>
> R5RS Scheme lisp solution:
> http://xahlee.org/UnixResource_dir/writ/Sourav_Mukherjee_sourav.work_gmail.scm
> by Sourav Mukherjee
>
> also, a Common Lisp solution can be found here:
> http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/5d1ded8824bc750b?
It's too complex. Just write:
(let ((list '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n)
(2 o p) (4 q r) (5 s t))))
(mapcar (lambda (class) (reduce (function append) class :key (function rest)))
(com.informatimago.common-lisp.list:equivalence-classes list :key (function first)))
)
--> ((S T) (Q R M N) (G H) (O P K L E F) (I J C D) (A B))
--
__Pascal Bourguignon__ http://www.informatimago.com/
It's Xah. He cross-posts in an attempt to start a language feud.
Please don't feed the troll.
sherm--
--
Sherm Pendley
<http://camelbones.sourceforge.net>
Cocoa Developer
Xah is a cross-posting troll. Please don't feed the troll.
Unless you're going to talk about Perl, please take clpmisc out of the
xpost.
Ben
Clueless fuck. I unsubscribed from comp.lang.perl.misc to avoid the
retards like you and now you come in via the backdoor. If you have a
problem with Xah report him with Google Groups and with his hosting
provider 1&1 like I do. Dreamhost kicked him out that way.
--
John Bokma j3b
Blog: http://johnbokma.com/ Facebook: http://www.facebook.com/j.j.j.bokma
Freelance Perl & Python Development: http://castleamber.com/
Hi Paul,
thanks for your solution, and thanks to many other who provided
solutions. It'll take some time to go thru them.
btw, i disagree about your remark on crossposting. For those
interested, you can read in the following:
• 〈Cross-posting & Language Factions〉
http://xahlee.org/Netiquette_dir/cross-post.html
if anyone wants to argue with me about this, there's a my blog link at
the bottom of the page where you can leave a comment. Feel free to use
that.
i'll go over the solutions and post if i have anything interesting to
say. ☺ Possbly will select some to show on my site with credit of
course.
Xah ∑ xahlee.org ☄
sorry i disagree. And please don't randomly accuse... I posted the
following in reply to Paul Rubin's very valuable post, to
comp.lang.python only. But since you cross-posted your accusation, and
there are about 3 other posts of similar nature accusing me cross-
posted to all groups, so am posting a response to all groups too.
--------------------------------------------------
Paul,
...
In Q (from kx.com)[1]:
x:((0; "a"; "b");(1; "c"; "d");(2; "e"; "f");(3; "g"; "h");(1; "i"; "j")
(2; "k"; "l");(4; "m"; "n");(2; "o"; "p");(4; "q"; "r");(5; "s"; "t"))
f:{each [,/] each [each [1 _]] x @ value group x[;0]}
f x
outputs
"ab"
"cdij"
"efklop"
"gh"
"mnqr"
"st"
Note that this is actually a pretty general solution in that *all*
the functions used in it operate on a variety of types.
- The label could be anything, not just an integer.
- What follows a label can be a list of anything.
- Everything, except for = (the group function), has to do with
*shaping* the output in the way you want. It is all basically
cut-n-paste or pulling apart your Lego blocks and constructing
a new toy from them! And most languages are really poor at that.
*This* is why proponents of various languages should pay attention
to such problems.
----
[1] In k3 (an older language from kx.com that you may be able to find
online), x is the same but f is as follows:
f:{,/'1_''x@=x[;0]}
I have solved my problems with Xah years ago, that's what killfiles are
for. And I have no idea why you are bringing up Xah in your current
rant.
It was livibetter who without any motivation or reasoning posted Python
code in CLPM. If at least he had asked something like "How can I write
something similar in Perl?" or _ANYTHING_ that would have made this even
remotely meaningful for CLPM, then ok.
But he didn't. He just dumped 7 lines of Python code to CLPM and his
only comment was "Here is mine for Python". Yeah, great comment. Why
would anyone in CLPM possibly care about those 7 lines of Phython code?
jue
jue
> I have solved my problems with Xah years ago, that's what killfiles are
> for. And I have no idea why you are bringing up Xah in your current
> rant.
>
> It was livibetter who without any motivation or reasoning posted Python
> code in CLPM. If at least he had asked something like "How can I write
> something similar in Perl?" or _ANYTHING_ that would have made this even
> remotely meaningful for CLPM, then ok.
> But he didn't. He just dumped 7 lines of Python code to CLPM and his
> only comment was "Here is mine for Python". Yeah, great comment. Why
> would anyone in CLPM possibly care about those 7 lines of Phython code?
Check the Newsgroups line: this is a standard Xah "cross-post to
everywhere in sight" troll. Hence (a) bringing up Xah, since he
originated this thread, and (b) livibetter not thinking to trim the
crossposts. Which you didn't either, I note.
The only downside to killfiles is that you have to pay attention or
you sometimes get bitten by this stuff.
--
Rhodri James *-* Wildebeest Herder to the Masses
> here's a interesting toy list processing problem.
>
> I have a list of lists, where each sublist is labelled by a number. I
> need to collect together the contents of all sublists sharing the same
> label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>
> [...]
>
> anyone care to give a solution in Python, Perl, javascript, or other
> lang? am guessing the scheme solution can be much improved... perhaps
> using some lib but that seems to show scheme is pretty weak if the lib
> is non-standard.
In Haskell the solution looks like this:
import qualified Data.Map as M
import qualified Data.Set as S
import Data.Map (Map)
import Data.Set (Set)
collect :: (Ord a, Ord k) => [Map k (Set a)] -> Map k (Set a)
collect = M.unionsWith S.union
Greets,
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
> In Haskell the solution looks like this:
>
> [...]
And before anyone starts to rant, I didn't pay attention to where I'm
X-posting this stuff. Sorry for that. But on the other hand you could
say that I'm giving the Perl people (apparently the only people feeling
the urge to rant anyway) the chance to come up with a more elegant
solution. =)
I am hijacking the following post and driving it to Cuba (the Monthy
Python fans will know what I refer to). I want to create a `reduce'-
like function that can handle similar problems.
Xah said:
> here's a interesting toy list processing problem.
>
> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing
> the same label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>
> stuffed deleted.
Here is my Common Lisp (and I only care about Common Lisp answers)
attempt to create a `reduce'-like function to handle this kind of a
problem (you will see that I am still struggling with the code and the
documentation).
(defun reduce-tagged (function sequence &key
(key-tag #'first)
(key-datum #'rest))
"Use a binary operation, `function' to combine a sequence of tagged
elements. like-tagged elements are `reduce'd according to `function'
and returned in a list ...
`sequence' is a sequence of tagged elements. reduce-m will reduce
like-tagged-elements.
If `key-tag' is supplied it is used to extract the element tag. If
`key-tag' is not supplied, the function `first' is used.
If `key-datum' is supplied, it is used to extract the element datum.
If `key-datum' is not supplied, the function `rest' is used.
"
(let ((hash (make-hash-table)))
(dolist (datum sequence)
(let ((tag (funcall key-tag datum))
(values (funcall key-datum datum)))
(multiple-value-bind (it present)
(gethash tag hash)
(if present
(setf (gethash tag hash)
(apply function (gethash tag hash) values))
(setf (gethash tag hash) values)))))
(let (result)
(dohash (key value hash)
(push (list key value) result))
result)))
Comments, improvements? I am looking for a general purpose function
like reduce that I
can apply in various situations.
Thanks,
Mirko
Correction: the previous code used a non-portable clisp macro
`dohash' (looks nice, doesn't it?)
Here is the version with maphash:
(defun reduce-tagged (function sequence &key
(key-tag #'first)
(key-datum #'rest))
"Use a binary operation, `function' to combine a sequence of tagged
elements. like-tagged elements are `reduce'd according to `function'
`sequence' is a sequence of tagged elements. reduce-m will reduce
like-tagged-elements.
If `key-tag' is supplied it is used to extract the element tag. If
`key-tag' is not supplied, the function `first' is used.
If `key-datum' is supplied, it is used to extract the element datum.
If `key-datum' is not supplied, the function `rest' is used.
"
(let ((hash (make-hash-table)))
(dolist (datum sequence)
(let ((tag (funcall key-tag datum))
(values (funcall key-datum datum)))
(multiple-value-bind (it present)
(gethash tag hash)
(declare (ignore it))
(if present
(setf (gethash tag hash)
(apply function (gethash tag hash) values))
(setf (gethash tag hash) values)))))
(let (result)
(maphash #'(lambda(key value)
(push (list key value) result))
hash)
result)))
Mirko
Aaand one more fix (apply -> funcall) (This version at least produces
a close
facsimile of the desired output)
(defun reduce-tagged (function sequence &key
(key-tag #'first)
(key-datum #'rest))
"Use a binary operation, `function' to combine a sequence of tagged
elements. like-tagged elements are `reduce'd according to `function'
`sequence' is a sequence of tagged elements. reduce-m will reduce
like-tagged-elements.
If `key-tag' is supplied it is used to extract the element tag. If
`key-tag' is not supplied, the function `first' is used.
If `key-datum' is supplied, it is used to extract the element datum.
If `key-datum' is not supplied, the function `rest' is used.
"
(let ((hash (make-hash-table)))
(dolist (datum sequence)
(let ((tag (funcall key-tag datum))
(values (funcall key-datum datum)))
(multiple-value-bind (it present)
(gethash tag hash)
(declare (ignore it))
(if present
(setf (gethash tag hash)
(funcall function (gethash tag hash) values))
Here is a solution in Perl -- the verbose version. Please see my note
below.
SCRIPT:
use strict;
use warnings;
my %lists;
while (<DATA>)
{
chomp;
my ($k, @v) = split(/ /, $_);
push(@{$lists{$k}}, @v);
}
foreach my $k (sort keys %lists)
{
print "$k - @{$lists{$k}}\n";
}
exit(0);
__DATA__
0 a b
1 c d
2 e f
3 g h
1 i j
2 k l
4 m n
2 o p
4 q r
5 s t
OUTPUT:
>perl lists.plx
0 - a b
1 - c d i j
2 - e f k l o p
3 - g h
4 - m n q r
5 - s t
NOTE:
I assume that you want an idiomatic solution for the language. I have
therefore converted your data into a typical record oriented
structure. Perlistas don't use parenthesis. If you want a Lispy
solution, use Lisp. Further, Perl was made for exactly this kind of
problem, which is simple data munging, taking some input, transforming
it, and printing it out -- Practical Extraction and Reporting
Language. I know that some Lispers (R.G., are you reading?) will
object to a formulation like this: @{$lists{$k}}, but all this says
(in Perl) is to spit out the value contained in the hash element
$lists{$k} as an array, and is good idiomatic Perl, even if some
Lispers aren't quite up to the task of understanding it.
CC.
> Here is my Common Lisp (and I only care about Common Lisp answers)
Good for you. So why are you spamming other newsgroups with your CL
solution? Not once, but three times.
Replies to /dev/null.
--
Steven
> btw, i disagree about your remark on crossposting.
You're wrong. Crossposting is indeed a standard trolling tactic.
This does not prove that all crossposters are trolls, nor does it assert
that all crossposters are trolls, but it is absolutely factually correct
that it's a standard trolling tactic.
-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
Not exactly; he posted it in a crossposted thread, which happened to include
CLPM and other groups, including comp.lang.python.
It is quite possible that he didn't know about the crossposting. However,
while I would agree that this would constitute a form of ignorance, I'd think
that, especially with how well some newsreading interfaces hide that detail,
I don't think it's really worth getting angry over.
> On 2010-09-26, Xah Lee <xah...@gmail.com> wrote:
>> On Sep 25, 11:17??pm, Paul Rubin <no.em...@nospam.invalid> wrote:
>>> Python solution follows (earlier one with an error cancelled). ??All
>>> crossposting removed since crossposting is a standard trolling tactic.
>
>> btw, i disagree about your remark on crossposting.
>
> You're wrong.
FYI: Xah Lee is well known for his abuse of cross posting. Be happy that
Google Groups limits the number of groups to crosspost to five.
Related: his spamming behaviour has already resulted in Xah Lee
having to look for another hosting provider [1]. Currently 1and1 provides
him shelter, most likely carefully selected by Xah (as Google Groups)
since 1and1 is not known for their spam fighting reputation nor clue.
But one can only hope, so if you want to do something about this Xah
thing, please report it with 1and1 and ab...@google.com. He won't learn
respect from it, but maybe you end up being honored [2] on his
collection of drivel [3].
In short:
= don't reply to Xah Lee: at best it's a waste of time
= if his post is abusive (crossposting to 5 groups just because you can
is) report it with /and/ his hosting provider (since most of his
posts are copy paste jobs of articles on his site just to drive
traffic) and Google Groups (his Usenet provider of choice since they
hardly do a thing about spam).
[1] http://www.mail-archive.com/pytho...@python.org/msg91631.html
[2] http://www.google.com/search?q=site%3Axahlee.org%20bokma
[3] What's sad is that some of its stuff is actually good/not bad.
But tainted: Xah Lee is a spammer and a Usenet abuser.
> On 2010-09-26, J?rgen Exner <jurg...@hotmail.com> wrote:
>> It was livibetter who without any motivation or reasoning posted Python
>> code in CLPM.
>
> Not exactly; he posted it in a crossposted thread, which happened to include
> CLPM and other groups, including comp.lang.python.
>
> It is quite possible that he didn't know about the crossposting.
Oh, he does. It has been Xah's game for years.
> while I would agree that this would constitute a form of ignorance, I'd think
> that, especially with how well some newsreading interfaces hide that detail,
> I don't think it's really worth getting angry over.
You think wrong. And Jurgen should have known better than to reply several
times (but like too many people in cplm he posts for posting's sake, the
main reason why I don't follow that group anymore).
Xah has been doing this for many years and most of his posts are just
made to drive traffic to his site (hence they are copy paste of articles
on his site + link(s) to his site). It's Usenet abuse, on purpose.
The reason it pisses off people is that nearly weekly it causes a lot of
noise in newsgroups that are really better off without the noise
(IMNSHO).
See my other post on how to deal with this spammer. If you've missed it:
report him for spamming, since that's what he does. It already made him
have to move hosting providers once. While it's not going to stop him,
it will cost him money. See:
http://www.google.com/search?q=site%3Axahlee.org%20bokma
While I am named in that article be assured that I was not the only one
contacting dreamhost (+10 for doing this, btw). Quite some people
contacted me via email that they also talked with Dreamhost. Just keep
reporting this spammer, and maybe 1and1 will kick it out.