Combining sets/dictionaries

  • Thread starter Alfons Nonell-Canals
  • Start date
A

Alfons Nonell-Canals

Hello,
I have different sets/dictionaries/lists (whatever you want because I
can convert them easily) and I would like to combine them. I don't want
a consensus and something like it. I'd need to combine all elements of
the first one with the all elements of the second one and third,... the
numbers of dictionaries/sets/lists is variable as the number of elements
for each one.

For example, i have the following sets and I would like to obtain all
possible combinations...

['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']

And it should be flexible because as I've said, the number of
dictionaries/lists/sets is not always the same, as the number of elements.

I don't like to ask this kid of questions but... today I'm totally lost
and I need it to close one part of a nice project...

Thanks in advance!

Best regards,
Alfons.





--
------------
Alfons Nonell-Canals, PhD
Chemogenomics Lab
Research Group on Biomedical Informatics (GRIB) - IMIM/UPF
Barcelona Biomedical Research Park (PRBB)
C/ Doctor Aiguader, 88 - 08003 Barcelona
(e-mail address removed) - http://cgl.imim.es
Tel. +34933160528

http://alfons.elmeuportal.cat
http://www.selenocisteina.info
 
P

Peter Otten

Alfons said:
I have different sets/dictionaries/lists (whatever you want because I
can convert them easily) and I would like to combine them. I don't want
a consensus and something like it. I'd need to combine all elements of
the first one with the all elements of the second one and third,... the
numbers of dictionaries/sets/lists is variable as the number of elements
for each one.

For example, i have the following sets and I would like to obtain all
possible combinations...

['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']

And it should be flexible because as I've said, the number of
dictionaries/lists/sets is not always the same, as the number of elements.

I don't like to ask this kid of questions but... today I'm totally lost
and I need it to close one part of a nice project...

Using one common definition of "combinations":
[['I', 'O', 'N', 'P', 'S', 'C'],
['I', 'O', 'N', 'P', 'S', 'C'],
['I', 'O', 'N', 'P', 'S', 'C'],
['I', 'N', 'P', 'S', 'C'],
['I', 'N', 'P', 'S', 'C'],
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C'],
['I', 'O', 'N', 'P', 'S', 'C'],
['I', 'O', 'N', 'P', 'S', 'C'],
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']].... print c
....
(['I', 'O', 'N', 'P', 'S', 'C'], ['I', 'O', 'N', 'P', 'S', 'C'])
(['I', 'O', 'N', 'P', 'S', 'C'], ['I', 'O', 'N', 'P', 'S', 'C'])
(['I', 'O', 'N', 'P', 'S', 'C'], ['I', 'N', 'P', 'S', 'C'])
(['I', 'O', 'N', 'P', 'S', 'C'], ['I', 'N', 'P', 'S', 'C'])
<snip>

If you mean something else (likely) please give a more detailed description
or provide some examples with both input and desired output.

Peter
 
A

Adrien

Hi,

Did you try the list.update() builtin function ?

Regards

Peter Otten a écrit :
Alfons said:
I have different sets/dictionaries/lists (whatever you want because I
can convert them easily) and I would like to combine them. I don't want
a consensus and something like it. I'd need to combine all elements of
the first one with the all elements of the second one and third,... the
numbers of dictionaries/sets/lists is variable as the number of elements
for each one.

For example, i have the following sets and I would like to obtain all
possible combinations...

['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']

And it should be flexible because as I've said, the number of
dictionaries/lists/sets is not always the same, as the number of elements.

I don't like to ask this kid of questions but... today I'm totally lost
and I need it to close one part of a nice project...

Using one common definition of "combinations":
[['I', 'O', 'N', 'P', 'S', 'C'],
['I', 'O', 'N', 'P', 'S', 'C'],
['I', 'O', 'N', 'P', 'S', 'C'],
['I', 'N', 'P', 'S', 'C'],
['I', 'N', 'P', 'S', 'C'],
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C'],
['I', 'O', 'N', 'P', 'S', 'C'],
['I', 'O', 'N', 'P', 'S', 'C'],
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']]... print c
...
(['I', 'O', 'N', 'P', 'S', 'C'], ['I', 'O', 'N', 'P', 'S', 'C'])
(['I', 'O', 'N', 'P', 'S', 'C'], ['I', 'O', 'N', 'P', 'S', 'C'])
(['I', 'O', 'N', 'P', 'S', 'C'], ['I', 'N', 'P', 'S', 'C'])
(['I', 'O', 'N', 'P', 'S', 'C'], ['I', 'N', 'P', 'S', 'C'])
<snip>

If you mean something else (likely) please give a more detailed description
or provide some examples with both input and desired output.

Peter
 
M

Mel

Alfons said:
Hello,
I have different sets/dictionaries/lists (whatever you want because I
can convert them easily) and I would like to combine them. I don't want
a consensus and something like it. I'd need to combine all elements of
the first one with the all elements of the second one and third,... the
numbers of dictionaries/sets/lists is variable as the number of elements
for each one.

For example, i have the following sets and I would like to obtain all
possible combinations...

['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']

And it should be flexible because as I've said, the number of
dictionaries/lists/sets is not always the same, as the number of elements.

I don't like to ask this kid of questions but... today I'm totally lost
and I need it to close one part of a nice project...

Maybe a recursive function or generator:

Each item from the first list appended to each of the possible combinations
from the second through nth lists.

Mel.
 
C

Carl Banks

Hello,
I have different sets/dictionaries/lists (whatever you want because I
can convert them easily) and I would like to combine them. I don't want
a consensus and something like it. I'd need to combine all elements of
the first one with the all elements of the second one and third,... the
numbers of dictionaries/sets/lists is variable as the number of elements
for each one.

For example, i have the following sets and I would like to obtain all
possible combinations...

['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']

This is the input. What do you want the output to look like given
this input?

I can't really tell exactly what you mean by "all combinations", there
are different ways to combine sequences, and it doesn't sound like you
mean the formal mathematical definition of "combination".

My best guess is that you want something like this.

def all_union(seqs):
s = set()
for seq in seqs:
s.update(seq)
return s


Carl Banks
 
A

Alfons Nonell-Canals

Hello,
finally I've solved it using a "combinatorics" library which allows to
do this kind of things.

Here, here is an example:


http://automatthias.wordpress.com/2007/04/28/cartesian-product-of-multiple-sets/


Thanks for your suggestions.


Regards,
Alfons.

Carl said:
Hello,
I have different sets/dictionaries/lists (whatever you want because I
can convert them easily) and I would like to combine them. I don't want
a consensus and something like it. I'd need to combine all elements of
the first one with the all elements of the second one and third,... the
numbers of dictionaries/sets/lists is variable as the number of elements
for each one.

For example, i have the following sets and I would like to obtain all
possible combinations...

['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['I', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['I', 'O', 'N', 'P', 'S', 'C']
['F', 'I', 'L', 'O', 'N', 'P', 'S', 'R', 'C']

This is the input. What do you want the output to look like given
this input?

I can't really tell exactly what you mean by "all combinations", there
are different ways to combine sequences, and it doesn't sound like you
mean the formal mathematical definition of "combination".

My best guess is that you want something like this.

def all_union(seqs):
s = set()
for seq in seqs:
s.update(seq)
return s


Carl Banks

--
------------
Alfons Nonell-Canals, PhD
Chemogenomics Lab
Research Group on Biomedical Informatics (GRIB) - IMIM/UPF
Barcelona Biomedical Research Park (PRBB)
C/ Doctor Aiguader, 88 - 08003 Barcelona
(e-mail address removed) - http://cgl.imim.es
Tel. +34933160528

http://alfons.elmeuportal.cat
http://www.selenocisteina.info
 
A

alex23

Alfons Nonell-Canals said:
finally I've solved it using a "combinatorics" library which allows to
do this kind of things.

If you're using a version of Python > 2.6 you might find that
itertools.combinations does what you want without requiring the
additional code.
 
A

alex23

If you're using a version of Python > 2.6 you might find that
itertools.combinations does what you want without requiring the
additional code.

Actually, that's probably _not_ what you want, sorry. I don't like the
recursive solution you've found, though, especially as you can get the
same result with a generator expression:
set1 = set(['smart', 'dumb']) # the example on the site is easier to follow than your use case, sorry
set2 = set(['hard-working', 'lazy'])
list((a,b) for a in set1 for b in set2)
[('smart', 'lazy'), ('smart', 'hard-working'), ('dumb', 'lazy'),
('dumb', 'hard-working')]
set3 = ['crazy', 'sane', 'boring'] # works with iterators of any size
list((a,b) for a in set1 for b in set3)
[('smart', 'crazy'), ('smart', 'sane'), ('smart', 'boring'), ('dumb',
'crazy'), ('dumb', 'sane'), ('dumb', 'boring')]

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top