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


Messages In This Thread
Group List Elements according to the Input with the order of binary combination - by quest_ - Jan-26-2021, 08:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 197 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  difference between forms of input a list to function akbarza 6 1,042 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  unable to remove all elements from list based on a condition sg_python 3 452 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Copying the order of another list with identical values gohanhango 7 1,168 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 484 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,354 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Checking if a string contains all or any elements of a list k1llcod3 1 1,110 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,075 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 2,006 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,159 May-17-2022, 11:38 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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