0

Python itertools.product module not working as expect when I'm extracting string "[1,2], [3,4], [5,6,7], [8,9]" from a json object here:

},
 "combos_matrix": "[1,2], [3,4], [5,6,7], [8,9]"
},

But it's working when I put directly numbers in ():

list(itertools.product([1,2], [3,4], [5,6,7], [8,9]))

Here is my results when I extract data from json and try the function

Json sample:

},
"combos_matrix": "[1,2], [3,4], [5,6,7], [8,9]"
},

Code by using json object:

combos_matrix = map_json["world"]["combos_matrix"]
print(combos_matrix)
combos_list = list(itertools.product(combos_matrix))
print(combos_list)

Output:

[1,2], [3,4], [5,6,7], [8,9]
[('[',), ('1',), (',',), ('2',), (']',), (',',), (' ',), ('[',), ('3',), (',',), ('4',), (']',), (',',), (' ',), ('[',), ('5',), (',',), ('6',), (',',), ('7',), (']',), (',',), (' ',), ('[',), ('8',), (',',), ('9',), (']',)]

What I'm expecting and this is the result when I give the function directly the list of numbers:

Code:

combos_list = list(itertools.product([1,2], [3,4], [5,6,7], [8,9]))
print(combos_list)

Output:

[(1, 3, 5, 8), (1, 3, 5, 9), (1, 3, 6, 8), (1, 3, 6, 9), (1, 3, 7, 8), (1, 3, 7, 9), (1, 4, 5, 8), (1, 4, 5, 9), (1, 4, 6, 8), (1, 4, 6, 9), (1, 4, 7, 8), (1, 4, 7, 9), (2, 3, 5, 8), (2, 3, 5, 9), (2, 3, 6, 8), (2, 3, 6, 9), (2, 3, 7, 8), (2, 3, 7, 9), (2, 4, 5, 8), (2, 4, 5, 9), (2, 4, 6, 8), (2, 4, 6, 9), (2, 4, 7, 8), (2, 4, 7, 9)]

Can you help on this guy, I guess I'm missing something.

2
  • 1
    If it's under your control, you might want to fix the way the data gets created: you probably want `combos_matrix to be a list of lists rather than a string Commented Mar 24, 2024 at 21:18
  • What does the documentation of the data say about the format of that string? Commented Mar 24, 2024 at 22:09

2 Answers 2

1

Extracting combos_matrix from JSON objects makes it a string representation of the list, but you need the actual list, you can get it from json.loads():

combos_matrix = map_json["world"]["combos_matrix"]
print(combos_matrix)

combos_matrix_json = f'[{combos_matrix}]'

combos_list = json.loads(combos_matrix_json)

combos_result = list(itertools.product(*combos_list))
print(combos_result)

Output:

[1,2], [3,4], [5,6,7], [8,9]
[(1, 3, 5, 8), (1, 3, 5, 9), (1, 3, 6, 8), (1, 3, 6, 9), (1, 3, 7, 8), (1, 3, 7, 9), (1, 4, 5, 8), (1, 4, 5, 9), (1, 4, 6, 8), (1, 4, 6, 9), (1, 4, 7, 8), (1, 4, 7, 9), (2, 3, 5, 8), (2, 3, 5, 9), (2, 3, 6, 8), (2, 3, 6, 9), (2, 3, 7, 8), (2, 3, 7, 9), (2, 4, 5, 8), (2, 4, 5, 9), (2, 4, 6, 8), (2, 4, 6, 9), (2, 4, 7, 8), (2, 4, 7, 9)]
Sign up to request clarification or add additional context in comments.

Comments

1

The type of the dictionary value is string, so you need to convert this string to a list first (NOTE: your string is missing the initial [ and final ], so when converting this string to a list it needs to be added).

For string->list conversion you can use ast.literal_eval:

from ast import literal_eval
from itertools import product

dct = {"combos_matrix": "[1,2], [3,4], [5,6,7], [8,9]"}

lst = literal_eval(f"[{dct['combos_matrix']}]")
for p in product(*lst):
    print(p)

Prints:

(1, 3, 5, 8)
(1, 3, 5, 9)
(1, 3, 6, 8)
(1, 3, 6, 9)
(1, 3, 7, 8)
(1, 3, 7, 9)
(1, 4, 5, 8)
(1, 4, 5, 9)
(1, 4, 6, 8)
(1, 4, 6, 9)
(1, 4, 7, 8)
(1, 4, 7, 9)
(2, 3, 5, 8)
(2, 3, 5, 9)
(2, 3, 6, 8)
(2, 3, 6, 9)
(2, 3, 7, 8)
(2, 3, 7, 9)
(2, 4, 5, 8)
(2, 4, 5, 9)
(2, 4, 6, 8)
(2, 4, 6, 9)
(2, 4, 7, 8)
(2, 4, 7, 9)

5 Comments

I think it would be good to say why you convert it to a list first, instead of evaluating it to a tuple.
I mean, someone might think you could just do the simpler literal_eval(dct['combos_matrix']), without realizing why you didn't do that.
@nocomment I've updated my answer and put a note about it there.
Hmm, that explains a detail of the list conversion, not why you decided to convert to a list. Now I wonder whether you actually do have a reason for that other than that is the only thing you thought of. Do you realize that using literal_eval(dct['combos_matrix']) does work here? And do you realize why it's still bad?
In case someone ever wonders: the issue with literal_eval(dct['combos_matrix']) is that it wouldn't work if there weren't four sublists but only one or zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.