How to generate all combinations of group lists at the same time?

Hi, everyone

I have a group lists like this (more lines are omitted:

a b c d 
E F G H
....

I want to generate all combinations of two elements in each group line. What I expected is this:

a b
a c
a d
b c
b d
c d
E F
E G
E H
F G
F H
G H
..

Any programing language is appreciated.

Instead of just saying you're a beginner, why don't you put a little effort into this and try to solve it on your own. If you get stuck, show us what you have tried and explain where you got stuck.

1 Like

Ok, I searched Google and tried python:

import itertools
paralog = ['a','b','c','d']
for L in range(2, len(paralog)-1):
  for subset in itertools.combinations(paralog, L):
    print(subset)

It just give me one group, and I have more than one thousand groups.

My python is non-existent, but I guess what you post just uses an array of constants and doesn't read and loop your input file.
And, I don't think Don Cragun was talking of copying google results into here, but trying to compose something on your own after (hopefully) having learned from the suggestions you received in here.
Try

awk '{for (i=1; i<=NF; i++) for (j=i+1; j<=NF; j++) print $i, $j}' file
a b
a c
a d
b c
b d
c d
E F
E G
E H
F G
F H
G H
2 Likes

Run as python combination.py nengcheng.file

import fileinput
import itertools

for line in fileinput.input():
    fields = line.split()
    for L in range(2, len(fields)-1):
        for subset in itertools.combinations(fields, L):
            print ' '.join(subset)

Run as perl combination.pl nengcheng.file

while(<>) {
    my @fields = split;
    while(@fields) {
        my $lead = shift @fields;
        for(@fields) {
            print "$lead $_\n";
        }
    }
}
1 Like

Thanks for your advice, I understand what you mean. Frankly speaking, I majoring biology and need to analyze some datasets. If I used the scripts in my paper, maybe I could list your name as co-author or in the acknowledgement.

$ 
$ cat list_comb.txt
a b c d
E F G H
$ 
$ perl -lne 'sub comb {return if !$#_;
                 print for glob join "", map "{$_}", map {join ",", @$_}([shift],\@_);
                 comb(@_)}
             comb(split)' list_comb.txt
ab
ac
ad
bc
bd
cd
EF
EG
EH
FG
FH
GH
$ 
$ 
1 Like

Thanks for the offer, but don't bother. We're here to offer help and learn together. And seeing someone, e.g. a biologist, improve her/his coding skills is one of the rewards we take away. This was the meaning of my remark, and certainly Don Cragun's as well.

2 Likes

Hi.

At our shop we often like to produce generalized solutions. For the case of combinations and permutations, a perl module:

Math::Combinatorics can produce:

      use Math::Combinatorics;
      my @n = qw(a b c);
      print join("\n", map { join " ", @$_ } combine(2,@n)),"\n";

    Output:

      a b
      a c
      b c

For any set of strings that you get into array n .

That allows easy parameterization of generating combinations. Of course, the infrastructure code takes up some room, so utilities can grow to a hundred lines. For many this would not be worth the effort, but consider how easy it is to produce something like the demonstration script:

#!/usr/bin/env bash

# @(#) s1       Demonstrate combinations from CL, file.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C combination permutation my-columns dixf

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, read from command line, combinations of 2:"
combination -2 a b c d

pl " Results, read from command line, combinations of 3:"
combination -3 a b c d

pl " Results, read from file, columnized:"
combination -2 < $FILE |
my-columns

pl " Results, read from file, columnized, better separation:"
combination -2 < $FILE |
my-columns -s='/ / | /'

pl " Results, read from command line, but permutations, columnized:"
permutation a b c d |
my-columns -s='/ / | /'

pl " Details of combination:"
dixf combination

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
combination (local) 1.6
permutation (local) 1.1
my-columns (local) 1.6
dixf (local) 1.42

-----
 Input data file data1:
a b c d 
E F G H

-----
 Results, read from command line, combinations of 2:
b c
b d
b a
c d
c a
d a


-----
 Results, read from command line, combinations of 3:
a c d
a c b
a d b
c d b


-----
 Results, read from file, columnized:
a b a d a F a H b d b F b H c E c G d E d G E F E H F H
a c a E a G b c b E b G c d c F c H d F d H E G F G G H

-----
 Results, read from file, columnized, better separation:
a b | a E | a H | b E | b H | c F | d E | d H | E H | G H
a c | a F | b c | b F | c d | c G | d F | E F | F G
a d | a G | b d | b G | c E | c H | d G | E G | F H

-----
 Results, read from command line, but permutations, columnized:
a b c d | a c d b | b a c d | b c d a | c a b d | c b d a | d a b c | d b c a
a b d c | a d b c | b a d c | b d a c | c a d b | c d a b | d a c b | d c a b
a c b d | a d c b | b c a d | b d c a | c b a d | c d b a | d b a c | d c b a

-----
Details of combination:

combination     Produce n combinations of arguments or file: [-n] a1 a2 ... am (what)
Path    : ~/bin/combination
Version : 1.6
Length  : 172 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/env perl
Help    : probably available with -h,--help
Modules : (for perl codes)
 warnings       1.23
 strict 1.08
 English        1.09
 Carp   1.3301
 Data::Dumper   2.151_01
 Getopt::Long   2.42
 feature        1.36_01
 Math::Combinatorics    0.09

We currently don't post our local codes, but we provide some details on what is done, and how it is accomplished.

Best wishes ... cheers, drl