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
#13
My variables was just a single list. I'm not completely sure, but maybe you want to run it for each of the groups in your product? Like this?

from itertools import product
from more_itertools import grouper
import string

# create the "data"
number = 2
for variables in product([0, 1], repeat=(number*2)):
    print(f"Running for variables = {variables}")

    groups = grouper(variables, 2)
    finallist = product(*groups)
    print(list(finallist))
Output:
Running for variables = (0, 0, 0, 0) [(0, 0), (0, 0), (0, 0), (0, 0)] Running for variables = (0, 0, 0, 1) [(0, 0), (0, 1), (0, 0), (0, 1)] Running for variables = (0, 0, 1, 0) [(0, 1), (0, 0), (0, 1), (0, 0)] Running for variables = (0, 0, 1, 1) [(0, 1), (0, 1), (0, 1), (0, 1)] ...
Reply


Messages In This Thread
RE: Group List Elements according to the Input with the order of binary combination - by bowlofred - Jan-27-2021, 07:54 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 220 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  difference between forms of input a list to function akbarza 6 1,097 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  unable to remove all elements from list based on a condition sg_python 3 478 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Copying the order of another list with identical values gohanhango 7 1,190 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 508 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,376 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Checking if a string contains all or any elements of a list k1llcod3 1 1,134 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,091 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 2,034 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,205 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