Reputation: 847
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
).
Upvotes: 1
Views: 11385
Reputation:
The following will move items from one list to another without the incorrect iterator position issue you were facing in the original problem:
c1 = [10, 45, 50, 75, 90]
c2 = [15, 25, 30, 65, 80]
while c1:
c2.append(c1[0])
del c1[0]
print (c1, c2)
Upvotes: 0
Reputation: 5914
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
Upvotes: 1
Reputation: 80
If you want to create every possible allocation of 10 items to 2 lists, then I would use combinations in the itertools package. For example:
import itertools
items = [10, 25, 45, 50, 15, 30, 65, 75, 80, 90]
for m in xrange(len(items)+1):
combinations = list(itertools.combinations(items, m))
for c1 in combinations:
c1 = list(c1)
c2 = list(set(items) - set(c1))
print c1, c2
Upvotes: 0
Reputation: 207
Try:
c1.append(c2.pop(i))
c1.sort()
OR
c2.append(c1.pop(i))
c2.sort()
where:
Upvotes: 4