I want to explore every possible community allocation of 10 nodes. I have total 10 items: 10 15 25 30 45 50 65 75 80 90
There are two lists (communities) c1
and c2
that I will allocate these items. Initially, I split the 10 items like following:
c1 = [10, 45, 50, 75, 90] c2 = [15, 25, 30, 65, 80]
Now I want to move one item to another list like:
c1 = [45, 50, 75, 90] c2 = [10, 15, 25, 30, 65, 80]
c1 = [10, 45, 50, 75] c2 = [15, 25, 30, 65, 80, 90]
...
I also want to move two items, three items, four items, (but not five items). Like,
c1 = [50, 75, 90] c2 = [10, 15, 25, 30, 45, 65, 80]
c1 = [10, 75, 90] c2 = [15, 25, 30, 45, 50, 65, 80]
...
c1 = [75, 90] c2 = [10, 15, 25, 30, 45, 50, 65, 80]
c1 = [10, 90] c2 = [15, 25, 30, 45, 50, 65, 75, 80]
...
c1 = [90] c2 = [10, 15, 25, 30, 45, 50, 65, 75, 80]
c1 = [45] c2 = [10, 15, 25, 30, 50, 65, 75, 80, 90]
...
I want to move every possible iterations of 1-4 items from c1
to c2
. (Total 31 possibilities: 2^5-1
) The order inside each list doesn't matter. How can I do this?
I used the following code.
c1 = [10, 45, 50, 75, 90]
c2 = [15, 25, 30, 65, 80]
for i in c1:
c2.append(i)
c1.remove(i)
print c1, c2
With this code, I can only get following result. This code didn't accomplish the task of moving one item to c2
. My code didn't attempt to move multiple items to c2
.
[45, 50, 75, 90] [15, 25, 30, 65, 80, 10]
[45, 75, 90] [15, 25, 30, 65, 80, 10, 50]
[45, 75] [15, 25, 30, 65, 80, 10, 50, 90]
How can I successfully finish the task of moving items to c2
? With this task, I can get every possible allocation of 10 items to two lists (ignoring cases c1==c2
).
As far as I understand you are more interested in the algorithm instead of simply appending from one list to another.
There is a standard library function which provides combinations of an iterable.
It is really a good exercise to make your own combinations
function.
Quick and dirty solution to your problem:
import itertools
c1 = [10, 45, 50, 75, 90]
c2 = [15, 25, 30, 65, 80]
print c1, c2
for i in range(1, 5):
for c in itertools.combinations(c1, i):
mc1 = sorted(list(set(c1).difference(set(c))))
mc2 = sorted(list(set(c2).union(c)))
print mc1, mc2
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments