How to move Items from one list to the another list in python?

user3077008

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).

Mariy

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to move one element from a list to another in python

From Dev

How to filter one list of items from another list of items?

From Dev

How to copy only new items from one list to another

From Dev

How to create list of items from another list

From Dev

Python: How do I compare all items in one list as a string to all items in another list?

From Dev

How to transfer items of one static list to another

From Java

Replace one list of items with another list in all possible combinations in python

From Dev

Move One Element From one list to another In haskell

From Dev

Move items from one sortable connected list to the other programmatically

From Dev

How to move items/buttons from one div to another using JQuery?

From Dev

How to move a process from one process group to another, and how to list the processes in each process group?

From Dev

dragula JS move from one list to another with on click event

From Dev

Conditionally bulk move a list documents from one collection to another

From Dev

Move all files from one folder to another, based on a list

From Dev

Exclude items from one list based on second list in Python

From Dev

How to move file in another server from one list file using while read line?

From Dev

How to move a range of items in a list to the end of the list

From Dev

Copy all matching items from one list to another

From Dev

Cleaner Way to Take Items from One List to Another

From Dev

Fastest way of updating items of one list from another using StartsWith

From Dev

Firebase & javascript - exclude items from one list to another

From Dev

How to iterate a python list and compare items in a string or another list

From Dev

SenchaTouch - Move items from one panel to another

From Dev

How to compare one value from a list of lists to another value from a list of lists in Python

From Dev

How to assign from one list to another?

From Dev

How to read a List from one class in another

From Dev

How to send a list from a form to another one

From Dev

Excluding items from a list that exist in another list

From Dev

How to select a few items from the list in python?

Related Related

  1. 1

    how to move one element from a list to another in python

  2. 2

    How to filter one list of items from another list of items?

  3. 3

    How to copy only new items from one list to another

  4. 4

    How to create list of items from another list

  5. 5

    Python: How do I compare all items in one list as a string to all items in another list?

  6. 6

    How to transfer items of one static list to another

  7. 7

    Replace one list of items with another list in all possible combinations in python

  8. 8

    Move One Element From one list to another In haskell

  9. 9

    Move items from one sortable connected list to the other programmatically

  10. 10

    How to move items/buttons from one div to another using JQuery?

  11. 11

    How to move a process from one process group to another, and how to list the processes in each process group?

  12. 12

    dragula JS move from one list to another with on click event

  13. 13

    Conditionally bulk move a list documents from one collection to another

  14. 14

    Move all files from one folder to another, based on a list

  15. 15

    Exclude items from one list based on second list in Python

  16. 16

    How to move file in another server from one list file using while read line?

  17. 17

    How to move a range of items in a list to the end of the list

  18. 18

    Copy all matching items from one list to another

  19. 19

    Cleaner Way to Take Items from One List to Another

  20. 20

    Fastest way of updating items of one list from another using StartsWith

  21. 21

    Firebase & javascript - exclude items from one list to another

  22. 22

    How to iterate a python list and compare items in a string or another list

  23. 23

    SenchaTouch - Move items from one panel to another

  24. 24

    How to compare one value from a list of lists to another value from a list of lists in Python

  25. 25

    How to assign from one list to another?

  26. 26

    How to read a List from one class in another

  27. 27

    How to send a list from a form to another one

  28. 28

    Excluding items from a list that exist in another list

  29. 29

    How to select a few items from the list in python?

HotTag

Archive