Python Forum
Group List Elements according to the Input with the order of binary combination
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Group List Elements according to the Input with the order of binary combination
#1
Hello,
I have this code scripts:
for variables in itertools.product([0, 1], repeat=(number*2)): # set of  variables for a,ap,b,bp,c,cp like 000000 000001 000011....
    #a,ap,b,bp,c,cp= variables[0],variables[1],variables[2],variables[3],variables[4],variables[5] #giving value for a,ap,b,bp,c,cp
    a,ap,b,bp= variables[0],variables[1],variables[2],variables[3]
    finallist = [(a,b),(a,bp),(ap,b),(ap,bp)] #binary combination of a,ap,b,bp,c,cp
    #finallist = [i for i in range(hidden_variables)]
    #finallist = [(a,b,c),(a,b,cp),(a,bp,c),(a,bp,cp),(ap,b,c),(ap,b,cp),(ap,bp,c),(ap,bp,cp)]
I know that "variables" is already an array but after that I want to group this list according to value of "number" instead of writing by hand . For example if "number" is 2, then list will have that: finallist = [(a,b),(a,bp),(ap,b),(ap,bp)] or if "number" is 3, the list will have that: finallist = [(a,b,c),(a,b,cp),(a,bp,c),(a,bp,cp),(ap,b,c),(ap,b,cp),(ap,bp,c),(ap,bp,cp)].In this code, first I am separating variables as a ap b bp.. and after that I am making binary combination according to "number". Instead of making this step by hand, I thought maybe it could be possible to do it dynamically and I wrote the following code script but it did not give the reult that I want:
subList = [variables[n:n+number] for n in range(0, len(variables*2), number)]
However it did not work!

For clarification:

This is my code for number 2:

number = 2
for variables in itertools.product([0, 1], repeat=(number*2)): # set of  variables for a,ap,b,bp,c,cp like 000000 000001 000011....
    a,ap,b,bp= variables[0],variables[1],variables[2],variables[3]
    finallist = [(a,b),(a,bp),(ap,b),(ap,bp)] 
THis is my code for number 3

number = 3
for variables in itertools.product([0, 1], repeat=(number*2)): # set of  variables for a,ap,b,bp,c,cp like 000000 000001 000011....
    a,ap,b,bp,c,cp= variables[0],variables[1],variables[2],variables[3],variables[4],variables[5] #giving value for a,ap,b,bp,c,cp
    finallist = [(a,b,c),(a,b,cp),(a,bp,c),(a,bp,cp),(ap,b,c),(ap,b,cp),(ap,bp,c),(ap,bp,cp)]
THis is my code for number =4

number = 4
for variables in itertools.product([0, 1], repeat=(number*2)): # set of  variables for a,ap,b,bp,c,cp like 000000 000001 000011....
    a,ap,b,bp,c,cp,d,dp= variables[0],variables[1],variables[2],variables[3],variables[4],variables[5] ,variables[6],variables[7]#giving value for a,ap,b,bp,c,cp
    finallist = [(a,b,c,d),(a,b,c,dp),(a,b,cp,d),....]#I am not writing 
And I am looking for a way to create finallist dynamically
Reply
#2
Quote:However it did not work!
Please elaborate and post any error messages ( complete and unaltered in error tags )
Reply
#3
(Jan-26-2021, 10:19 PM)Larz60+ Wrote:
Quote:However it did not work!
Please elaborate and post any error messages ( complete and unaltered in error tags )

I should get this result(and with old method I can get this result)
[(0, 0), (0, 0), (0, 0), (0, 0)]
[(0, 0), (0, 1), (0, 0), (0, 1)]
[(0, 1), (0, 0), (0, 1), (0, 0)]
[(0, 1), (0, 1), (0, 1), (0, 1)]
[(0, 0), (0, 0), (1, 0), (1, 0)]
[(0, 0), (0, 1), (1, 0), (1, 1)]
[(0, 1), (0, 0), (1, 1), (1, 0)]
[(0, 1), (0, 1), (1, 1), (1, 1)]
[(1, 0), (1, 0), (0, 0), (0, 0)]
[(1, 0), (1, 1), (0, 0), (0, 1)]
[(1, 1), (1, 0), (0, 1), (0, 0)]
[(1, 1), (1, 1), (0, 1), (0, 1)]
[(1, 0), (1, 0), (1, 0), (1, 0)]
[(1, 0), (1, 1), (1, 0), (1, 1)]
[(1, 1), (1, 0), (1, 1), (1, 0)]
[(1, 1), (1, 1), (1, 1), (1, 1)]
However when I try to do this code, dynamically(with subList script), I am getting this result:
[(0, 0), (0, 0), (), ()]
[(0, 0), (0, 1), (), ()]
[(0, 0), (1, 0), (), ()]
[(0, 0), (1, 1), (), ()]
[(0, 1), (0, 0), (), ()]
[(0, 1), (0, 1), (), ()]
[(0, 1), (1, 0), (), ()]
[(0, 1), (1, 1), (), ()]
[(1, 0), (0, 0), (), ()]
[(1, 0), (0, 1), (), ()]
[(1, 0), (1, 0), (), ()]
[(1, 0), (1, 1), (), ()]
[(1, 1), (0, 0), (), ()]
[(1, 1), (0, 1), (), ()]
[(1, 1), (1, 0), (), ()]
[(1, 1), (1, 1), (), ()]
Reply
#4
To achieve list as you present, what are the values for number?
Reply
#5
(Jan-27-2021, 02:36 AM)Larz60+ Wrote: To achieve list as you present, what are the values for number?
Number (for these values ) is an integer and its value is 2 for list that I presented above. So I have 4 variable(number*2) and I will have pair binary combinations of these 4 variables (a,b),(a,bp),(ap,b),(ap,bp)

If number would be 3, this time I would have 6 variable and as a list I would have the triple binary combination of these 6 variables : (a,b,c),(a,b,cp),(a,bp,c),(a,bp,cp),(ap,b,c),(ap,b,cp),(ap,bp,c),(ap,bp,cp)
Reply
#6
I'm not sure where the variables are coming from, but you can pass them into itertools.product to get the list that you want from them.

>>> from itertools import product
>>> list(product(("a","ap"), ("b", "bp"), ("c", "cp")))
[('a', 'b', 'c'), ('a', 'b', 'cp'), ('a', 'bp', 'c'), ('a', 'bp', 'cp'), ('ap', 'b', 'c'), ('ap', 'b', 'cp'), ('ap', 'bp', 'c'), ('ap', 'bp', 'cp')]
Reply
#7
This is the problem already. I do not want to write these variables by hand!
Probably I could not explain my problem to you

I just want to do these 2 line dynamically:

a,ap,b,bp= variables[0],variables[1],variables[2],variables[3]
finallist = [(a,b),(a,bp),(ap,b),(ap,bp)] 
These a ap b bp are coming from this line

for variables in itertools.product([0, 1], repeat=(number*2)): # set of  variables for a,ap,b,bp,c,cp like 000000 000001 000011....
"variables" includes actually a ap b bp...

Ok I am writing from the beginning
This is my code for number 2:

number = 2
for variables in itertools.product([0, 1], repeat=(number*2)): # set of  variables for a,ap,b,bp,c,cp like 000000 000001 000011....
    a,ap,b,bp= variables[0],variables[1],variables[2],variables[3]
    finallist = [(a,b),(a,bp),(ap,b),(ap,bp)] 
THis is my code for number 3

number = 3
for variables in itertools.product([0, 1], repeat=(number*2)): # set of  variables for a,ap,b,bp,c,cp like 000000 000001 000011....
    a,ap,b,bp,c,cp= variables[0],variables[1],variables[2],variables[3],variables[4],variables[5] #giving value for a,ap,b,bp,c,cp
    finallist = [(a,b,c),(a,b,cp),(a,bp,c),(a,bp,cp),(ap,b,c),(ap,b,cp),(ap,bp,c),(ap,bp,cp)]
THis is my code for number =4

number = 4
for variables in itertools.product([0, 1], repeat=(number*2)): # set of  variables for a,ap,b,bp,c,cp like 000000 000001 000011....
    a,ap,b,bp,c,cp,d,dp= variables[0],variables[1],variables[2],variables[3],variables[4],variables[5] ,variables[6],variables[7]#giving value for a,ap,b,bp,c,cp
    finallist = [(a,b,c,d),(a,b,c,dp),(a,b,cp,d),....]#I am not writing 
And I am looking for a way to create finallist dynamically
Reply
#8
Your information is already stored in the list variables. I wouldn't move the data to other names.

Maybe something like this?
from itertools import product
from more_itertools import grouper
import string

# create the "data"
number_pairs = 3
variables = list(string.ascii_uppercase[:number_pairs * 2])
print(f"variables data starts as {variables}")

number = len(variables)

groups = grouper(variables, 2)
finallist = product(*groups)
print(list(finallist))
Output:
variables data starts as ['A', 'B', 'C', 'D', 'E', 'F'] [('A', 'C', 'E'), ('A', 'C', 'F'), ('A', 'D', 'E'), ('A', 'D', 'F'), ('B', 'C', 'E'), ('B', 'C', 'F'), ('B', 'D', 'E'), ('B', 'D', 'F')]
Change number_pairs make a bigger list, and the the product is created from whatever is in the list.
Reply
#9
(Jan-27-2021, 04:40 PM)bowlofred Wrote: Your information is already stored in the list variables. I wouldn't move the data to other names.

Maybe something like this?
from itertools import product
from more_itertools import grouper
import string

# create the "data"
number_pairs = 3
variables = [x for x in string.ascii_uppercase[:number_pairs * 2]]
print(f"variables data starts as {variables}")

number = len(variables)

groups = grouper(variables, 2)
finallist = product(*groups)
print(list(finallist))
Output:
variables data starts as ['A', 'B', 'C', 'D', 'E', 'F'] [('A', 'C', 'E'), ('A', 'C', 'F'), ('A', 'D', 'E'), ('A', 'D', 'F'), ('B', 'C', 'E'), ('B', 'C', 'F'), ('B', 'D', 'E'), ('B', 'D', 'F')]
Change number_pairs make a bigger list, and the the product is created from whatever is in the list.

My variables are not string. They are integer! So I changed the code anf it did not worked!
for variables in itertools.product([0, 1], repeat=(number*2)): # set of  variables for a,ap,b,bp,c,cp like 000000 000001 000011....
    groups = grouper(variables, 2)
    finallist2 =list( product(*groups))
    print(finallist2)
I got this error

Error:
TypeError: 'int' object is not iterable
Reply
#10
What's the full error? What is the error telling you is an int instead of an iterable?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why am I getting list elements < 0 ? Mark17 8 485 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 267 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 390 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Make Groups with the List Elements quest 2 387 Jul-11-2021, 09:58 AM
Last Post: perfringo
  I cannot delete and the elements from the list quest 4 822 May-11-2021, 12:01 PM
Last Post: perfringo
  List of lists - merge sublists with common elements medatib531 1 808 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Find Common Elements in 2 list quest 4 792 Apr-14-2021, 03:57 PM
Last Post: quest
  Order a list with successive permutations based on another list yvrob 3 753 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  User input/picking from a list AnunnakiKungFu 2 685 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
  Sorting Elements via parameters pointing to those elements. rpalmer 3 791 Feb-10-2021, 04:53 PM
Last Post: rpalmer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020