I'm a beginner in Python and I'm trying to understand how the iterations in for loop work.
I have created a multidimensional tuple that contains integer like below
image_dimension = ((820, 312),
(1500, 500),
(2480, 520))
Now, I would like to use the index of every row of the tuple as an index in a for
cycle.
I have tried the code for i in image_dimension:
but clearly, it returns the tuple of every row and not the index
At the moment I'm using the following code to access the positions of the tuple
cycles = range(0, 3)
for count in cycles:
aspect_ratio_x = image_width / image_dimension[count][0]
to_crop_from_high_and_low = image_dimension[count][1] * aspect_ratio_x
# do other stuff
but instead of using the range()
function and manually change the element inside it, I would like the for
loop automatically iterate the row of the tuple.
I know I could use something like cycles = range(0, len(image_dimension))
but I would like to make it cleaner
As suggested by @alani the solution is to use the enumerate()
function
for index, (i, j) in enumerate(image_dimension):
aspect_ratio_x = image_width / image_dimension[index][0]
to_crop_from_high_and_low = image_dimension[index][1] * aspect_ratio_x
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.