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
#11
Just make variables an iterator/list whatever of integers then. Here I just set it via range, but it could be set any way.

from itertools import product
from more_itertools import grouper
import string

# create the "data"
number_pairs = 3
variables = list(range(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 [0, 1, 2, 3, 4, 5] [(0, 2, 4), (0, 2, 5), (0, 3, 4), (0, 3, 5), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5)]
Reply
#12
(Jan-27-2021, 06:03 PM)bowlofred Wrote: Just make variables an iterator/list whatever of integers then. Here I just set it via range, but it could be set any way.

from itertools import product
from more_itertools import grouper
import string

# create the "data"
number_pairs = 3
variables = list(range(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 [0, 1, 2, 3, 4, 5] [(0, 2, 4), (0, 2, 5), (0, 3, 4), (0, 3, 5), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5)]

I apologise, probably it is my fault that I cannot tell what I want exactly but NO! my variables are not 0 2 4 my variables must be that
for variables in itertools.product([0, 1], repeat=(number*2))
After that I should create this groups
And after that I am writing what you recommand:
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,c,cp= variables[0],variables[1],variables[2],hvariables[3] #giving value for a,ap,b,bp,c,cp
    #nlist = [(a,b),(a,bp),(ap,b),(ap,bp)] #binary combination of a,ap,b,bp,c,cp
    groups = grouper(variables, 2)
    finallist = product(*groups)
    print(list(finallist))
And I am getting this error:
Error:
raceback (most recent call last): File "free.py", line 151, in <module> groups = grouper(variables, 2) File "/usr/lib/python3/dist-packages/more_itertools/recipes.py", line 295, in grouper args = [iter(iterable)] * n TypeError: 'int' object is not iterable
My variable is already a list !!! Why am I gettigng this error?

P.S I also wrote: variable = list(variable) but still it did not work
Reply
#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
#14
(Jan-27-2021, 07:54 PM)bowlofred Wrote: 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)] ...
Exactly but still I have the same error??
Error:
groups = more_itertools.grouper(variables, 2) File "/usr/lib/python3/dist-packages/more_itertools/recipes.py", line 295, in grouper args = [iter(iterable)] * n TypeError: 'int' object is not iterable
This is working for you but not working for me. This is quite interesting!
Reply
#15
Show the entire code (including imports). Your code seems to work for me....

import itertools
import more_itertools

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,c,cp= variables[0],variables[1],variables[2],hvariables[3] #giving value for a,ap,b,bp,c,cp
    #nlist = [(a,b),(a,bp),(ap,b),(ap,bp)] #binary combination of a,ap,b,bp,c,cp
    groups = more_itertools.grouper(variables, 2)
    finallist = itertools.product(*groups)
    print(list(finallist))
Output:
[(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)]
Reply
#16
(Jan-27-2021, 08:38 PM)bowlofred Wrote: Show the entire code (including imports). Your code seems to work for me....

import itertools
import more_itertools

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,c,cp= variables[0],variables[1],variables[2],hvariables[3] #giving value for a,ap,b,bp,c,cp
    #nlist = [(a,b),(a,bp),(ap,b),(ap,bp)] #binary combination of a,ap,b,bp,c,cp
    groups = more_itertools.grouper(variables, 2)
    finallist = itertools.product(*groups)
    print(list(finallist))
Output:
[(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)]

Quite interesting.. since morning I am trying the make it works but no. Here is the my code:

from typing import Union, Sequence, Tuple, Any
import itertools
import cirq
import numpy as np
import random
import string
from collections import defaultdict
import csv
import pprint
import fileinput
import cvxpy 
from cirq.type_workarounds import NotImplementedType
import collections
import more_itertools

...
for variables in itertools.product([0, 1], repeat=(number*2)): # set of deterministic hidden variables for a,ap,b,bp,c,cp like 000000 000001 000011....
    print(f"Running for variables = {variables}")
    groups_ = more_itertools.grouper(variables, 2)
    finallist = itertools.product(*groups_)
    print(list(finallist))
...
I am using ubuntu and I installed more_itertools like that: sudo apt install python3-more-itertools. And the error is that:
Error:
Traceback (most recent call last): File "free.py", line 150, in <module> groups_ = more_itertools.grouper(variables, 2) File "/usr/lib/python3/dist-packages/more_itertools/recipes.py", line 295, in grouper args = [iter(iterable)] * n TypeError: 'int' object is not iterable
Reply
#17
Ah ok!

indexess will change their order:
Instead of following line
    groups_ = more_itertools.grouper(variables, 2)
we should write that:

    groups_ = more_itertools.grouper(2,variables)
Reply
#18
Very interesting. Is that a different package? I'm not on ubuntu, so just installed via pip.

For this package, the iterable is the first argument.

>>> import inspect
>>> import more_itertools
>>> inspect.signature(more_itertools.grouper)
<Signature (iterable, n, fillvalue=None)>
Reply
#19
(Jan-27-2021, 10:05 PM)bowlofred Wrote: Very interesting. Is that a different package? I'm not on ubuntu, so just installed via pip.

For this package, the iterable is the first argument.

>>> import inspect
>>> import more_itertools
>>> inspect.signature(more_itertools.grouper)
<Signature (iterable, n, fillvalue=None)>
I did not check the version explicitly but yeah it made me very angry. Luckily I found. Btw I found from documents ... Maybe they just updated ...
Reply
#20
I was interested. According to github, the original description of this function (in the python itertools recipe docs) was with n as the first argument.

The package was created in 2012, the docs "fixed" the signature so the iterator was the first argument in 2013, and then nobody noticed or fixed it in the package until 2019. So the old signature is in more_itertools up through 5.0.0 and the new one since 6.0.0. Looks like the ubuntu package installs 4.2.0 with the "old" signature.
quest_ likes this post
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 266 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 807 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 684 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