I have a simple question in python. How can I store arrays inside a tuple in Python. For example:
I want the output of my code to be like this:
JavaScript
x
2
1
bnds = ((0, 1), (0, 1), (0, 1), (0, 1))
2
So I want (0, 1)
to be repeated for a specific number of times inside a tuple!
I have tried to use the following code to loop over a tuple:
JavaScript
1
6
1
g = (())
2
for i in range(4):
3
b1 = (0,1) * (i)
4
g = (g) + (b1)
5
print(g)
6
However, the output is :
JavaScript
1
2
1
(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1)
2
Maybe this is a simple question but I am still beginner in python!
Any help!
Advertisement
Answer
You could create a list, fill it up, then convert it to a tuple.
JavaScript
1
8
1
g = []
2
b1 = (0, 1)
3
4
for i in range(4):
5
g.append(b1)
6
g = tuple(g)
7
print(g)
8
There are cleaner ways to do it, but I wanted to adapt your code in order to help you understand what is happening.